//首先装载一个配置类
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);

配置类这样写
@Configuration  //等同与xml中的<beans>标签
@ComponentScan("com.pkg") //扫描包下的所有类
public class MyConfig {

}

茴字的另一种写法

@Configuration
public class MyConfig {
    @Bean
    public SomeService someService() {
        return new SomeService();
    }
}

@Component
@Scope("prototype")
//Singleton:表示该Bean是单例模式,在Spring容器中共享一个Bean的实例
//Prototype:每次调用都会新创建一个Bean的实例
//Request:这个是使用在Web中,给每一个http request新建一个Bean实例
//Session:这个同样是使用在Web中,表示给每一个http session新建一个Bean实例
public class SomeService {
        ....
}

//使用BEAN
SomeService bean = context.getBean(SomeService.class);

另各种注解
@Controller用于标注控制层组件
@Service用于标注业务层组件
@Repository用于标注数据访问组件,即DAO组件
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
级别Controller> Service > Repository

Spring EL

@Configuration
@ComponentScan("org.sang")
@PropertySource(value = "some.properties",encoding = "UTF-8")
public class ELConfig {
        //直接赋值
    @Value("String value")
    private String normal;
    
    //使用java代码
    @Value("#{T(java.lang.Math).random()*100}")
    private double randomNumber;
    
    //加载文本
    @Value("file.txt")
    private Resource testFile;
    
    //some.properties 中的值
    @Value("${ppp.value}")
    private String su;
    
    //代码
    @Value("#{T(java.lang.Math).random()*100}")
    private double randomNumber;
    
}

两种方法初始化和销毁
@Configuration
public class MyConfig {
    @Bean(initMethod = "init",destroyMethod = "destroy")
    BeanWayService beanWayService() {
        return new BeanWayService();
    }
    @Bean
    JSR250WayService jsr250WayService() {
        return new JSR250WayService();
    }
}

public class JSR250WayService {
    @PostConstruct//构造方法执行之后执行
    public void init() {
        System.out.println("JSR250WayService-init()");
    }

public JSR250WayService() {
        System.out.println("JSR250WayService-构造方法");
    }
    @PreDestroy//销毁之前执行
    public void destroy() {
        System.out.println("JSR250WayService-destroy()");
    }
}

@Profile设置初始化选项,类似的注解有@Conditional
 
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().setActiveProfiles("prod");
    context.register(ProfileConfig.class);
    context.refresh();//需要刷新
    
    
    @Configuration
    public class ProfileConfig {
    @Bean
    @Profile("dev")
    public DemoBean devDemoBean() {
        return new DemoBean("dev");
    }

@Bean
    @Profile("prod")
    public DemoBean prodDemoBean() {
        return new DemoBean("prod");
    }
    }

//计划任务
@EnableScheduling//开启对计划任务的支持

@Scheduled(cron = "0 51 20 * * ?")
    public void fixTimeExecution() {
     .....
    }
    
组合注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@ComponentScan
public @interface MyConfiguration {
    String[] value();
}

@Configuration
@ComponentScan("com.pkg")
等同于
@MyConfiguration("com.pkg")

文章来源http://blog.csdn.net/u012702547

spring用注解配置,不用XML的更多相关文章

  1. Spring的注解配置与XML配置之间的比较

    注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作. 如:使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...

  2. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  3. spring aop注解配置

    spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...

  4. JavaWeb_(Spring框架)注解配置

    系列博文 JavaWeb_(Spring框架)xml配置文件  传送门 JavaWeb_(Spring框架)注解配置 传送门 Spring注解配置 a)导包和约束:基本包.aop包+context约束 ...

  5. spring aop注解方式与xml方式配置

    注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...

  6. 死磕Spring之AOP篇 - Spring AOP注解驱动与XML配置

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. Spring纯注解配置

    待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...

  8. SpringMVC基础配置(通过注解配置,非xml配置)

    SpringMVC是什么,有多火,我这里就不再啰嗦了,SpringMVC比Struts2好用太多,我在学校的时候私下里两种都接触过,对比之后果断选择了SpringMVC,后来在做Android应用开发 ...

  9. java框架之Spring(2)-注解配置IOC&AOP配置

    注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...

  10. Spring自定义注解配置切面实现日志记录

    一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...

随机推荐

  1. 华硕飞马3S,日常使用续航测试

    最近爱机荣耀6的电池1天2充,无奈换台新机,华为系列没大电池且价格贵,小米红米系列品控呵呵,其他品牌无小屏幕大容量电池: 然后换了台华硕飞马3S:5.2英寸 5000ma电池,日常工作娱乐使用1天半多 ...

  2. onOptionsItemSelected、onMenuItemSelected、onContextItemSelected 区别

         1.在点击选项菜单(OptionsMenu:点击menu弹出的菜单)的菜单项时即调用了onMenuItemSelected 也调用了onOptionsItemSelected ,于是疑惑他们 ...

  3. python:函数初始

    一.函数 1.函数初始:函数就是封装一个功能 2.函数名,函数体,关键字,函数的返回值 def 关键字,定义一个函数 my_len 函数名书写规则和变量一样 def 与函数名中间一个空格 函数名(): ...

  4. 记一次KUBERNETES/DOCKER网络排障

    https://coolshell.cn/articles/18654.html 总结在前面: 1.kill -9杀死docker进程,系统一定是要遍历所有的docker子进程来一个一个发退出信号的, ...

  5. kettle大数据量读写mysql性能优化

       修改kettleDB连接设置 1. 增加批量写的速度:useServerPrepStmts=false  rewriteBatchedStatements=true  useCompressio ...

  6. 【学习】pandas 基础介绍说明 【pandas】

    本文来源于<利用python进行数据分析>中文版,大家有兴趣可以看原版,入门的东西得脚踏实地哈 1.pandas 数据结构介绍 首先熟悉它的两个主要数据结构,Series 和 DataFr ...

  7. spring 之 类型转换 2

    spring内置的转换器 在spring xml 文件中,配置属性的时候, 不管实际是 list 还是map ,还是Date, 或者原生的java 类型, 我们只能配置xml 给它们. 那么 spri ...

  8. final关键字特点

    一.final关键字修饰的类 无法被继承(即不能有子类) 二.final关键字修饰的方法不能被重写 三.final关键字修饰的变量成为常量(即不允许被修改) 开发中经常使用   

  9. tfs填坑那些事

    1.csdn下载  安装(步1:装 步2:激活 步3:配置数据库之类 选择完全配置)激活(win7能出来,win10不出来) 注意 tfs服务开启,,代理服务开启 2.选择敏捷模板 3.无法新建项目, ...

  10. win10 下安装 neo4j(转)

    1.neo4j介绍 neo4j是基于Java语言编写图形数据库.图是一组节点和连接这些节点的关系.图形数据库也被称为图形数据库管理系统或GDBMS.详细介绍可看Neo4j 教程 2.安装Java jd ...