Spring注解和XML方式区别详解
因为做项目一直都是扫描包注解,通过Autowired注入调用,不清楚注入的方式,以及配置与注解的具体区别,所以通过这个文章记录一下
完全xml配置方式
首先定义一个product类,该类里面有id,name,以及category对象的set get方法,
Category类有id和name属性,如果某个属性比如(category的name)如果没有set方法,在a
pplicationContext.xml文件bean的整个property属性会报错,
如果set方法名没有和property的name名字对上,则property的name属性也会报错
package com.wudi;
public class Product {
private int id;
private String name;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
public class Category {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在application.xml中创建bean对象,
bean标签的name对应着spring容器中bean的名字,根据名字获取bean对象 ;
在product的bean对象中通过ref注入category的bean对象,如果不写ref,写value,则会报错,因为 value传值,ref传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:tx="http://www.springframework.org/schema/tx" 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/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<bean name="c" class="com.wudi.Category">
<property name="name" value="category 1" />
</bean>
<bean name="p" class="com.wudi.Product">
<property name="name" value="product1" />
<property name="category" ref="c" />
</bean>
</beans>
最后测试
package Test;
import com.wudi.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Product p = (Product) context.getBean("p");
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
}
}
半配置半注解方式,对象属性用注解配置,普通属性xml配置
注入一般的属性包括对象属性可以在bean的property属性中编写,但注入对象属性也可以不用在property中编写,可以通过注解的方式
删除xml中的
<property name="category" ref="c" />
再加上这一句,开启注解配置
<context:annotation-config/>,
四种方法:
将Autowired写在:
Product的category属性上;
Product中categeory属性的set方法上;
Product的无参构造方法上;
Product的只含有category参数的构造方法上(如果有多余的参数,则配置文件必须含这个参数的bean对象)
这样一来就将xml中的category的bean对象注入到product的bean对象中,最后测试结果同上
PS:
@Autowired是根据类型来注入bean对象的,可以和@Qualifier(“名字”)配合使用,
@Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参;
@Resourece既可以通过类型注入,也可以通过名字注入
可以参看下面文章
https://www.cnblogs.com/shz365/p/5088289.html
注解配置方式
在application.xml中什么都不写,只写上扫描语句<context:component-scan base-package="com.wudi"/>
<?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:tx="http://www.springframework.org/schema/tx" 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/aop
http://www.springframework.org/schema/aop/spring-aop-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">
<context:component-scan base-package="com.wudi"/>
</beans>
就意味着pojo包下的对象都被扫描纳入spring的管理中了,然后我们可以在对应的类上加上注解
@Component("p")
public class Product {
private int id;
private String name="product 1"
@Autowired
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
@Component("c")
public class Category {
private int id;
private String name="category 1";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后测试
小结
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和@Controller。在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用@Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
<context:annotation-config>
和 <context:component-scan>
的区别
<context:annotation-config>
是用于激活那些已经在spring容器里注册过的bean(无论是通过xml的方式还是通过package sanning的方式)上面的注解,是一个注解处理工具。
<context:component-scan>
除了具有<context:annotation-config>
的功能之外,<context:component-scan>
还可以在指定的package下扫描以及注册javabean 。