Java Spring-注解进行属性注入

2017-11-06 21:19:43

一、Spring 的注解装配 Bean
Spring2.5 引入使用注解去定义 Bean

  • @Component 描述 Spring 框架中 Bean

Spring 的框架中提供了与 @Component 注解等效的三个注解

  • @Repository 用于对 DAO 实现类进行标注 (dao 层)
  • @Service 用于对 Service 实现类进行标注 (service 层)
  • @Controller 用于对 Controller 实现类进行标注 (web 层)
1
2
3
4
5
6
7
//因为只有一个属性value,所以可以直接写。一般需要value="..."
@Component("user")
public class User {
    public void sayHello(){
        System.out.println("Hello World.");
    }
}

配置文件:

1
2
3
4
5
6
7
8
9
10
11
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
    <context:component-scan base-package="spring5"/>
 
 
</beans>

 

二、注解进行属性注入

普通属性:@Value(value="..."), 这时候可以不写 setter 方法

对象属性:@Resource(name = "....")

或者采用   @Autowired

       @Qualifier(value = "plane")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//因为只有一个属性value,所以可以直接写。一般需要value="..."
@Component("user")
public class User {
    @Value(value="Spring")
    private String s;
 
    public void sayHello(){
        System.out.println("Hello World.");
    }
 
    @Override
    public String toString() {
        return "User{" +
                "s='" + s + '\'' +
                '}';
    }
}

 

三、XML 和注解的混合使用

两种方式结合:一般使用 XML 注册 Bean, 使用注解进行属性的注入

首先介绍一些其他的注解配置:

(1) 配置 Bean 初始化方法和销毁方法 :
* init-method  和 destroy-method.

  • @PostConstruct  初始化
  • @PreDestroy 销毁
1
2
3
4
5
6
7
8
@PostConstruct
public void setup(){
System.out.println("初始化...");
}
@PreDestroy
public void teardown(){
System.out.println("销毁...");
}

(2) 配置 Bean 的作用范围 :@Scope

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component("user")
@Scope(value="prototype")
public class User {
    @Value(value="Spring")
    private String s;
 
    public void sayHello(){
        System.out.println("Hello World.");
    }
 
    @Override
    public String toString() {
        return "User{" +
                "s='" + s + '\'' +
                '}';
    }
}

 (3) 使用 Java 类来进行配置信息,在 XML 中扫描一下即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@Configuration
public class BeanConfig {
    @Bean(name = "car")
    public Car showCar() {
        Car car = new Car();
        car.setName("长安");
        car.setPrice(40000d);
        return car;
    }
 
    @Bean(name = "product")
    public Product initProduct() {
        Product product = new Product();
        product.setName("空调");
        product.setPrice(3000d);
        return product;
    }
}

混合使用范例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Person {
    @Autowired
    @Qualifier("car")
    private Car car;
    @Autowired
    @Qualifier(value = "plane")
    private Plane plane;
 
    @Override
    public String toString() {
        return "Person{" +
                "car=" + car +
                ", plane=" + plane +
                '}';
    }
}

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
    <!--使属性注入注解生效-->
    <context:annotation-config/>
 
    <bean id="car" class="spring6.Car"/>
    <bean id="plane" class="spring6.Plane"/>
 
    <bean id="person" class="spring6.Person">
    </bean>
 
</beans>

 

四、集成 Junit 测试

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class spring6 {
 
    @Autowired
    @Qualifier("person")
    private Person p;
 
    @Test
    public void demo(){
        System.out.println(p);
    }
}

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
 
    <!--使属性注入注解生效-->
    <context:annotation-config/>
 
    <bean id="car" class="spring6.Car"/>
    <bean id="plane" class="spring6.Plane"/>
 
    <bean id="person" class="spring6.Person">
    </bean>
 
</beans>

如果是注解进行的类注册,那么需要使用 <context:component-scan base-package="spring5"/> 来扫包,

不过一般 bean 的注册还是在 XML 文件中声明的,所以在注解方式中用的比较多的就是属性的注入操作了,这种操作只需要加上 <context:annotation-config/>,就可以使属性注入的注解生效。