Spring使用注解开发

1、配置依赖

  xml中扫描包并自动检测类并注册Bean定义【常用】

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描base-package并自动检测类并注册Bean定义-->
    <context:component-scan base-package="com.doubleh.pojo"/>
</beans>

  

2、注解

  2.1 @Component  -  定义在类上,注册为bean;等同于@Service、@Controller、@Repository

  2.2 @Scope  -  定义bean的作用域,作用在类上

  2.3 @Value()  -  定义在属性上,赋值

  2.4 @Autowired  -  定义在类的属性和set方法上

  2.5 @ComponentScan  -  扫描包,使用Java的方式配置beans时用。

    可以通过应用自定义过滤器来修改和扩展此行为,将它们添加为includeFilters注释的excludeFilters属性@ComponentScan(或XML配置中元素的<context:include-filter />或 <context:exclude-filter />子元素<context:component-scan>)。每个过滤器元素都需要typeexpression属性。

    

过滤器类型 范例表达 描述

注释(默认)

org.example.SomeAnnotation

在目标组件中的类型级别存在元存在的注释

可分配的

org.example.SomeClass

目标组件可分配给(扩展或实现)的类(或接口)。

方面

org.example..*Service+

目标组件要匹配的AspectJ类型表达式。

正则表达式

org\.example\.Default.*

要与目标组件的类名匹配的正则表达式。

习俗

org.example.MyTypeFilter

org.springframework.core.type.TypeFilter接口的自定义实现

 

@Configuration
@ComponentScan(basePackages = "org.example",
        includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
        excludeFilters = @Filter(Repository.class))
public class AppConfig {
    ...
}
<beans>
    <context:component-scan base-package="org.example">
        <context:include-filter type="regex"
                expression=".*Stub.*Repository"/>
        <context:exclude-filter type="annotation"
                expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>

3、完全使用Java的方式配置Spring

  连Spring的xml配置文件都不需要了。

  3.1 定义一个配置类:

//这个也会被Spring容器托管,注册到容器中,因为@Configuration也是一个@Component
//@Configuration代表这是一个配置类,等价于<beans></beans>.xml
@Configuration
//扫描com.doubleh.pojo包下的所有
@ComponentScan("com.doubleh.pojo")
//@Import(其他配置类)
public class appconfig { // 等价于<bean></bean> // 方法名就是bean id // 返回值就是bean class // @Bean // public Cat cat(){ // return new Cat(); // } }
    @ComponentScan("com.doubleh.pojo") 
      等价于 beans.xml 的
    <context:component-scan base-package="com.doubleh.pojo"/>  

 3.2 注册一个bean:

使用@ComponentScan 扫描的方式bean类上必须有注解声明,默认bean名是首字母小写的类名;如果在配置类中@Bean声明了,类上可以不写注解,一般不会省略和在配置类中写。
//注册为一个bean
@Component
public class Cat {
    private int id;
    private String name;

    @Value("2")
    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    @Value("李白")
    public void setName(String name) {
        this.name = name;
    }
} 

 3.3 使用AnnotationConfigApplicationContext

//如果完全使用配置类方式去做,我们就使用AnnotationConfigApplicationContext来获取上下文
 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(appconfig.class);
 Cat getCat = applicationContext.getBean("cat", Cat.class);
 System.out.println(getCat);

//通过
register注册配置类来获取上下文
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();

//第三种 @Component或带有JSR-330注释的类作为输入提供给构造函数
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();

//第四种 用的时候再去扫描
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.scan("com.acme");
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);

  

猜你喜欢

转载自www.cnblogs.com/xp2h/p/12375281.html