spring的Bean装配

Bean的装配

基于XML的装配

提供了两种基于XML的装配方式:设置注入和构造注入

基于XML的装配

在Spring实例化Bean的过程中,Spring首先会调用Bean的默认构造方法来实例化Bean对象,然后通过反射的方法调用setter方法来注入属性值。因此,设值注入要求一个Bean必须满足以下两点要求。

  1. Bean类必须提供一个默认的无参构造方法
  2. Bean类必须为需要注入的属性提供对应的setter方法

使用设值注入时,在Spring配置文件中,需要使用元素的子元素来为每一个属性注入值;而使用构造注入时,在配置文件中,需要

使用元素的子元素来定义构造方法的参数,可以使用其value属性(或子元素)来设置该参数的值。

基于注解(Annotation)的装配

Spring定义了一系列的注解,常用的如图所示

  1. @Component:可以使用此注解描述Spring中的Bean,但它是一个泛化的概念,仅仅表示一个组件,并且可以作用在任何层次。使用时只需要将该注解标注在相应类上即可。
  2. @Repository:用于将数据访问层(DAO)的类标识为Spring中的Bean,其功能与@Component相同。
  3. @Service:通常作用在业务层(Service),用于将业务层的类标识为Spring中的Bean,其功能与@Component相同
  4. @Controller:通常作用在控制层(如SpringMVC的Controller),用于将控制层的类标识为Spring中的Bean,其功能与@Component相同
  5. @Autowired:用于对Bean的属性变量、属性的setter方法以及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作。默认按照Bean的类型进行装配。
  6. @Resource:其作用与@Autowired一样。其区别在于@Autowired默认按照Bean类型装配,而@Resource默认按照Bean类型实例名称进行装配。@Resource中有两个重要属性:name和type。Spring将name属性解析为Bean实例名称,type属性解析为Bean实例类型。如果指定name属性,则按照实例名称进行装配;如果指定type属性,则按Bean类型进行装配;如果都不指定,则先按Bean实例名称进行装配,如果不能匹配,再按照Bean类型进行装配;如果都无法匹配,则抛出NoSuchBeanDefinitionException异常
  7. @Qualifier:与@Autowired注解配合使用,会将默认的按Bean类型装配修改为按Bean的实例名称装配,Bean的实例名称由@Qualifier注解的参数指定。

下面通过一个案例来演示如何通过这些注解来装配Bean:

  • 在chapter2项目的src目录下,创建一个com.itheima.annotation包,在该包中创建接口UserDao,并在接口中定义一个save()方法
package com.itheima.annotation;

public interface UserDao {
public void save();
}
  • 在com.itheima.annotation 包中,创建UserDao接口的实现类UserDaoImpl。该类需要实现接口中的save()方法
package com.itheima.annotation;

import org.springframework.stereotype.Repository;

@Repository("userDao")
public class UserDaoImpl implements UserDao{

	@Override
	public void save() {
		System.out.println("user......save");
	}

}

首先使用了@Repository注解将UserDaoImpl类标识为Spring中的Bean,其写法相当于配置文件中的编写。然后在save()方法中输出一句话,用于验证时是否成功调用了该方法。

  • 在com.itheima.annotation包中,创建接口UserService,在接口中同样定义一个save()方法
package com.itheima.annotation;

public interface UserService {
public void save();
}
  • 在com.itheima.annotation包中,创建UserService的接口UserServiceImpl
package com.itheima.annotation;

import org.springframework.stereotype.Repository;

@Repository("userService")
public class UserServiceImpl implements UserService{

	@Override
	public void save() {
		System.out.println("uservice.....save");
		
	}

}

在文件中,首先使用@Service注解将UserServiceImpl类表示为Spring中的Bean,这相当于配置文件中的编写;然后使用@Resource 注解标注在属性userDao上,这相当于配置文件中的写法;最后在该类的save()方法中调用userDao中的save()方法,并输出一句话。

  • 在com.itheima.annotation包中,创建控制器类UserController,编辑后如下所示
package com.itheima.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

@Controller("userController")
public class UserController {
@Resource(name="userService")
private UserService userService;
public void save() {
	this.userService.save();
	System.out.println("userController........save");
}
}

首先使用了@Controller注解标注了UserController类,这相当于在配置文件中编写; 然后使用了@Resource注解标注在userService属性上,这相当于在配置文件中编写最后在其save方法中调用了userService中的save()方法,并输出一句话

  • 在com.ithiema.annotation包中,创建配置文件beans6.xml,在配置文件中编写基于Annotation装配的代码,
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="http://www.springframework.org/schema/beans   
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/tx     
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    <!--使用context命名空间,在配置文件中开启相对应的注解处理器 -->
    <context :annnotation-config/>
    <!-- 分别定义3个实例-->
    <bean id ="userDao" class="com.itheima.annotation.UserDaoImpl"/>
    <bean id ="userService" class="com.itheima.UserServiceImpl"/>
    <bean id ="userController" class="com.itheima.UserController"/>
</beans>

从上述代码虽然不再需要配置子元素但仍需要在spring中配置相应的bean,可以用过通过**<context:component-scan base-package=“Bean所在路径”>**的方法对注解进行扫描从而避免配置bean对象。

有关的导入包:spring-aop-4.3.6 RELEASE.jar不加会产生java.lang.NoClassDefFoundError的错误

  • 在com.ithiema.annotation包中,创建测试类AnnotationAssembleTest ,在类中编写测试方法并定义配置文件的路径,然后通过Spring容器加载配置文件并获取UserController实例,最后调用实例中的save()方法

    package com.itheima.annotation;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AnnotationAssembleTest {
    public static void main(String[] args) {
    	String xmlPath= "com/itheima/annotation/beans6.xml";
    	ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);
    	UserController userController =(UserController)applicationContext.getBean("userController");
    	userController.save();
    }
    }
    

上述的注解@Resource用@Autowired也同样可以达到效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值