通过注解(annotation)配置Bean

时间:2023-03-08 21:23:02

Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning).

特定组件的注解包括:

    @Component:基本注解,标识了一个受spring管理的组件.

      @Repository:标识持久层组件

    @Service:标识服务层(业务层)组件

    @Controller:标识表现层组件

对于扫描上的组件,Spring有默认的命名策略,使用非限定类名,第一个字母小写,也可以在注解中通过value属性值表示组件的名称.

  当在组件上使用了特定的注解之后,还需要在spring的配置文件中声明:

 <context:component-scan base-package="com.wang" resource-pattern="dao/*.class"></context:component-scan>

  其中bese-package是必须的属性,resource-pattern是可选的属性

    bese-package:制定一个需要扫描的基类包Spring容器将会扫描这个基类包及其子类包中的所有类.当需要扫描多个包时,可以使用逗号隔开.

    resource-pattern:如果仅希望扫描特定的类,而非基包下的所有类,可以使用这个属性过滤特定的类.如上即表示只扫描com.wang的子包dao中的所有类.

组件装配:

  <context:component-scan>元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@autowired,@Resource,@Inject注解的属性.

使用@Autowired自动装配bean:

  @Autowired注解自动装配具有兼容类型的单个Bean属性:

    构造器,普通字段,一切具有参数的方法上都可以使用@autowired

    默认情况下,所有使用@Autowired注解的属性都需要被设置,当Spring找不到匹配的Bean装配属性时,会抛出异常,如果某一属性允许不被设置,可以设置@Autowired注解的required=false

    默认情况下,当IOC容器中存在多个类型兼容的bean时,通过类型的自动装配将无法工作,此时可以在@Qualifier注解里提供Bean的名称.

    @Autowired注解也可以应用在数组类型,集合属性,Map类型上.

下面通过一个例子,来应用一下上面的知识点,包结构和类如下,是一个简单的mvc结构对User实现增删改查的例子:

通过注解(annotation)配置Bean

User类:

//主要演示注解,这里不再定义属性
public class User { }

UserDao接口:

通过注解(annotation)配置Bean
public interface UserDao {

    public void add();
public void delete();
public void update();
public void search();
}
通过注解(annotation)配置Bean

UserDaoImpl类:

通过注解(annotation)配置Bean
@Repository
public class UserDaoImpl implements UserDao { @Override
public void add() {
System.out.println("添加用户");
}
@Override
public void delete() {
System.out.println("删除用户");
}
@Override
public void update() {
System.out.println("更新用户");
}
@Override
public void search() { System.out.println("查询用户");
} }
通过注解(annotation)配置Bean

UserService类:

通过注解(annotation)配置Bean
@Service
public class UserService { @Autowired
private UserDao userDao; public void add() {
userDao.add();
System.out.println("service add.."); }
public void delete() {
userDao.delete();
System.out.println("service delete.."); }
public void update() {
userDao.update();
System.out.println("service update.."); }
public void search() {
userDao.search();
System.out.println("service search.."); }
}
通过注解(annotation)配置Bean

UserAction类:

通过注解(annotation)配置Bean
@Controller
public class UserAction { @Autowired
private UserService userService; @Autowired(required=false)
private User user;
public String add(){ userService.add();
System.out.println("UserAction add..");
System.out.println(user);
return null;
}
public String delete(){ userService.delete();
System.out.println("UserAction delete..");
return null;
}
public String update(){ userService.update();
System.out.println("UserAction update..");
return null;
}
public String search(){ userService.search();
System.out.println("UserAction search..");
return null;
} }
通过注解(annotation)配置Bean

配置文件beans.xml:

通过注解(annotation)配置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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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-4.2.xsd">

<context:component-scan base-package="com.wang" /> </beans>
通过注解(annotation)配置Bean

测试代码:

通过注解(annotation)配置Bean
public class Test1 {

    public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); UserAction userAction=(UserAction) ctx.getBean("userAction");
userAction.add(); }
}
通过注解(annotation)配置Bean

打印结果:

添加用户
service add..
UserAction add..
null

因为在User类中,我们没有添加任何注解信息,而在UserAction类中定义属性user时,又在@Autowired中添加了required="false",所有打印出来的user为null.