最原始的Spring配置
public class Book {
private String name;
private double price;
//...... name和price的get/set方法
@Override
public String toString() {
return "Book [name=" + name + ", price=" + price + "]";
}
}
public class Cls {
private String classNo = "605";
//...... classNo的get/set方法
@Override
public String toString() {
return "Cls [classNo=" + classNo + "]";
}
}
public class Student {
private Book book;
private Cls cls;
//...... book和cls的get/set方法
@Override
public String toString() {
return "Student [book=" + book + ", cls=" + cls + "]";
}
}
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="boss" class="com.stu.Student">
<property name="book" ref="book" />
<property name="cls" ref="cls" />
</bean>
<bean id="book" class="com.stu.Book">
<property name="name" value="Spring学习笔记" />
<property name="price" value="199" />
</bean>
<bean id="cls" class="com.stu.Cls">
<property name="classNo" value="807" />
</bean>
</beans>
Test代码
public class AnnotationTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Book book = ctx.getBean("book", Book.class);
System.out.println(book);
}
}
使用注解
@Autowired
首先需要在xml中配置bean
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
然后在需要自动注入的变量上面添加注释@Autowired
public class Student {
@Autowired
private Book book;
@Autowired
private Cls cls;
@Override
public String toString() {
return "Student [book=" + book + ", cls=" + cls + "]";
}
}
还可以将@Autowired添加在set函数和构造函数上,效果等同.
在注入的时候没有发现Bean时会抛出NoSuchBeanDefinitionException,为了避免在没有找到Bean的时候不抛出异常,使用@Autowired(required = false)
简化beans.xml配置
使用<context:annotation-config/>替代<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/context
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/beans/spring-context.xsd">
<context:annotation-config />
<bean id="boss" class="com.stu.Student" />
<bean id="book" class="com.stu.Book">
<property name="name" value="Spring学习笔记" />
<property name="price" value="199" />
</bean>
<bean id="cls" class="com.stu.Cls">
<property name="classNo" value="807" />
</bean>
</beans>
到目前为止,还是需要在beans.xml里面配置bean
@Component、@Service、@Constroller注解的使用
@Component:表示一个Bean
@Service:功能与Component一样,一般在业务层使用
@Constroller:功能与Component一样,一般在控制层使用
@Component
@Component
public class Book {
}
@Component
public class Cls {
}
// 可以指定bean名
@Component("student")
public class Student {
}
使用Component还需要修改beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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.xsd">
<!-- 需要指定需要扫描的类包,寻找Component -->
<context:component-scan base-package="com.stu" />
</beans>
网友评论