通过注解的方式配置bean

 Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

即,想要实现注解的方式配置bean需要满足2个条件:

  1. 类上面标注特定组件,如:@Component、@Service、@Repository、@Controller
  2. 在配置文件中增加bean-scan:<context:component-scan base-package="com.pfSoft.annotation"></context:component-scan>

以下实例说明:新增People类,请注意命名空间(之前举例的时候都基本都略去了命名空间部分,但是通过bean-scan来实现基于注解的注入时,对于命名空间就比较重要了)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.pfSoft.annotation;
 
import org.springframework.stereotype.Component;
 
@Component
public class People {
    private String name;
    private Integer age;
    /**
     *
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     * @return the age
     */
    public Integer getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(Integer age) {
        this.age = age;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "People [name=" + name + ", age=" + age + "]";
    }
     
     
}

 

 新增接口和实现类:

1
2
3
4
5
package com.pfSoft.annotation.service;
 
public interface IPeopleManService {
    public Boolean Adult(Integer age);
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.pfSoft.annotation.service;
 
import org.springframework.stereotype.Service;
 
@Service(value="PeopleManService")
public class PeopleManServiceImp implements IPeopleManService {
 
    public Boolean Adult(Integer age) {
        Boolean ret=false;
        if(age>=18)
        {
            ret=true;
        }
        return ret;
    }
 
 
}

 测试方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class testAnnotation {
     
    ApplicationContext ctx=null;
    @Before
    public void init() {
        ctx=new  ClassPathXmlApplicationContext("spring-annotation.xml");
    }
     
    @Test
    public void test() {
        People people= (People) ctx.getBean("people");
        people.setAge(8);
        people.setName("pf");
        System.out.println(people);
        IPeopleManService service=(IPeopleManService) ctx.getBean("PeopleManService");
        if(service.Adult(people.getAge()))
        {
            System.out.println(MessageFormat.format("{0}成年了,{1}岁", people.getName(),people.getAge()));
        }
        else {
            System.out.println(MessageFormat.format("{0}未成年,只有{1}岁", people.getName(),people.getAge()));
        }
    }
}

 输出结果如下:

1
2
People [name=pf, age=8]
pf未成年,只有8

 

配置文件中的过滤

 resource-pattern实现过滤

<context:exclude-filter type="annotation" expression=""/>子节点表示要排除在外的目标类
<context:include-filter type="annotation" expression=""/>表示要包含的目标类

 

通过@Autowired自动装配bean

  • 默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean属性是,会跑出异常,若某一属性允许不被设置,可以设置@Autowired注解的required属性为false
  • 如果存在多个类型相同的bean时,可以使用@Qualifier 来确定使用哪个,或者在申明bean的注解上直接加上名字,保证属性名或者字段名一致。

 

posted on   falcon_fei  阅读(265)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示