1.为什么使用注解装配bean
首先了解一个问题,什么是装配?
装配:填装配置,给非简单数据类型(基本数据类型和String类型视为简单数据类型)属性设置指定值称为装配,所以我们知道了装配这个词是特指给非简单数据类型配置属性值的,简单数据类型没有装配这个概念说法。
在 Spring 中,尽管可以使用 XML 配置文件实现 Bean 的装配工作,但如果应用中 Bean 的数量较多,会导致 XML 配置文件过于臃肿,从而给维护和升级带来一定的困难。
Java 从 JDK 5.0 以后,提供了 Annotation(注解)功能,Spring 2.5 版本开始也提供了对 Annotation 技术的全面支持,我们可以使用注解来配置依赖注入。
Spring 默认不使用注解装配 Bean,因此需要在配置文件中添加 <context:annotation-config/>,启用注解。
2.回顾xml文件注册bean对象
2.1创建一个.xml文件并配置文档声明
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--没有autowire属性-->
<bean id="userMappingImpl" class="----.UserMappingImpl">
<property name="" value=""/>
<property name="" ref=""></property>
</bean>
<!--有autowire属性-->
<bean id="userMappingImpl" class="----.UserMappingImpl" autowire="byType">
<property name="" value=""/>
</bean>
<!--总结:autowire="五种不同方式"属性可以省略这行 <!--<property name="" ref=""></property> 代码,让spring容器根据指定方式自动实现配置来完成代码的注入(即所说的装配)-->
</beans>
其中autowire属性有以下参数
分别表示以不同的方式让spring容器注入依赖对象值。
2.2创建类
public class UserMapperImpl{
private UserMapper userMapper;
private String id;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
public void setId(String id){
this.id=id;
}
}
2.3
3.怎样使用注解装配bean
3.1摆脱.xml文件中的配置
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="让spring容器扫描的包名"/>
</beans>
<context:component-scan base-package=""/>作用:让容器自动扫描包下的注解,实现bean的创建,而哪些注解可以实现bean对象的创建呢?(@Component、@Repository、@Service、@Controller)
Spring主要的注解有:
@Component(组成部分、组件):使用该注解标注于类上即可,用来表示一个组件(Bean),
该注解等效于以下xml文件中单个bean对象配置
<bean id="" class=""></bean>
@Repository:用于将数据访问层(Dao层)上的类标识为spring中的bean,分工明确的告诉spring容器这是Dao层的类,功能与@Component相同。
@Service:用于业务层上的类标识为spring中的bean,功能与@Component相同。
@Controller:用于控制层上的类标识为为spring中的bean,功能与@Component相同(SpringMVC类注解时使用)。
我们可以记住:@Component相当于@Repository、@Service、@Controller的笼统说法,他们的功能都是一样的,例如我们有时候会把张三、李四笼统称为人一样。
使用@Component、@Repository、@Service、@Controller其中的注解修饰类后就表明在spring中注册了哪个类的实例对象到容器中。
做完这些之后我们只是简单的了解了有哪些注解可以用于实例bean,那么真正实现装配的有这几个注解:
@Autowired:自动装配,可作用于属性、Getter方法、构造方法,默认根据类型装配(autowire="byType")。
@Qualify:一般与@AutoWired注解配合使用,@Qualify(value="id"),可以使通过id名称来注入属性。
@Resource:作用于属性、Getter方法、构造方法,默认根据名称装配(autowire="byName")。
@Configuration:配置注解,可以建立一个空类,类上使用@Configuration注解标识该类为配置类使用。
@Configuration @ComponentScan(value = "包名")</*代替.xml文件的注解扫描*/ public class UserMapperImpl{}
创作不易,喜欢的小伙伴可以一键三连------再见!!!