spring—使用注解配置Bean

         Spring2.5开始,出现了注解装配JavaBean的新方式。注解可以减少代码的开发量,spring提供了丰富的注解功能,现在项目中注解的方式使用的也越来越多了。

 

** 开启注解扫描

         Spring容器默认是禁用注解配置的。打开注解扫描的方式主要有两种: <context:component-scan>组件扫描和<context:annotation-config>注解配置。

         一般选择第一种,因为第一种的功能比第二种强大的多。<context:annotation-config>用于激活那些已经在Spring容器中注册过的Bean,<context:component-scan>还可以在指定的package下扫描注册Bean的功能。

具体区别:http://www.cnblogs.com/leiOOlei/p/3713989.html

# <context:annotation-config>使用

先加入一个player类和team,需要在Player中的Team属性加上@autowire注解,因为Player类依赖于team类,当然也可以把@autowire属性注解放置在任何setter函数上

publicclass Player {

    private String name;

    privateintage;

扫描二维码关注公众号,回复: 1075128 查看本文章

    @Autowired

    private Team team;

   

    public Player() {}

   

    public Player(String name, intage, Team team) {                                                                                                                                           

       super();

       this.name = name;

       this.age = age;

       this.team = team;

    }

 

    public String getName() {

       returnname;

    }

   

    publicvoid setName(String name) {

       this.name = name;

    }

 

    publicint getAge() {

       returnage;

    }

 

    publicvoid setAge(intage) {

       this.age = age;

    }

 

    public Team getTeam() {

       returnteam;

    }

 

    publicvoid setTeam(Team team) {

       this.team = team;

    }

   

    @Override

    public String toString() {

       return"Player [name=" + name + ", age=" + age + ", team=" + team + "]";

    }

}

 

publicclass Team {

    private String name;

   

    public Team() {}

 

    public Team(String name) {

       this.name = name;

    }

 

    public String getName() {

       returnname;

    }

 

    publicvoid setName(String name) {

       this.name = name;

    }

 

    @Override

    public String toString() {

       return"Team [name=" + name + "]";

    }

}

spring的配置文件中配置

<!-- 开启注解配置 -->

<context:annotation-config></context:annotation-config>

<bean id="player" class="annotation.com.wxh.blog.Player" p:name="wxh" p:age="22"></bean>                                                                      

<bean id="team" class="annotation.com.wxh.blog.Team" p:name="boston"></bean>

结果:Player [name=wxh, age=22, team=Team[name=boston]] ==> success

# 使用更加强大的<context:component-scan>

开启注解扫描

<!--

       对于扫描到的组件,Spring会有默认的命名策略:第一个字母小写

        在组件上使用注解后需要在Spring的文件中声明<context:component-scan/>

        base-package属性指定一个需要扫描的基类包,Spring会扫描这个包及其子包的所有类

        当需要扫描多个包的时候用“,”分开

     -->

    <context:component-scan base-package="annotation.com.wxh.blog"></context:component-scan>                                                             

Player类和Team类中只需要在类的上面加上@Component注解就可以了。因为<context:component-scan>有注册bean的功能,@Component相当于在springIOC容器中注册了这个类。

Tips:

         >>springIOC容器中存在两个或者以上Team类的Bean是,自动装配会失败,因为默认的@autowiredbyType的,这时候我们需要@qualifer(“value”)指定选择哪个bean,这时自动装配就会很据byName装配。[qualifier:预选的]

         >>@component 中有value属性可以指定BeanId,其实在@service@Controller @repository中都有

 

** 特殊的注解标注

>> @Component: 通用的构造型注解,标识该类为spring组件[不推荐使用]

>> @Controller: 标识将该类定义为SpringMVC的控制器 ==> Controller

>> @Service: 标识将该类定义为业务服务==> Service

>> @Repository: 标识将该类定义为数据仓库==> Dao

 

猜你喜欢

转载自blog.csdn.net/u013468915/article/details/52460162