繁琐的xml配置有时候让人感到烦躁,而Spring支持的注解配置简化了bean的配置。
所以spring可以使用annotation进行主动注入以及自动检测bean。
<!--启用注解 -->
<context:annotation-config></context:annotation-config>
<!-- 自动检测pojo类为spring中的bean+启用注解 ,有了这个就可以不用配置-->
<context:component-scan base-package="com.lubby.test"></context:component-scan>
Course类
package com.lubby.test; import org.springframework.stereotype.Component; @Component public class Course { private String courseName = "Enlish"; private String teacherName; public Course() { super(); } public Course(String courseName, String teacherName) { super(); this.courseName = courseName; this.teacherName = teacherName; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } <span style="white-space:pre"> </span>public String getTeacherName() { return teacherName; } public void setTeacherName(String teacherName) { this.teacherName = teacherName; } @Override public String toString() { return "Course [courseName=" + courseName + ", teacherName=" + teacherName + "]"; } }
Student类
package com.lubby.test; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; @Component public class Student { private String name; private String school; private int age; @Resource(name = "course") private Course course; private String address; private List<String> teacherName; public Student() { super(); } public Student(String name, String school, int age, Course course, String address, List<String> teacherName) { super(); this.name = name; this.school = school; this.age = age; this.course = course; this.address = address; this.teacherName = teacherName; } public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSchool() { return school; } public void setSchool(String school) { this.school = school; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } public List<String> getTeacherName() { return teacherName; } public void setTeacherName(List<String> teacherName) { this.teacherName = teacherName; } @Override public String toString() { return "Student [name=" + name + ", school=" + school + ", age=" + age + ", course=" + course + ", address=" + address + ", teacherName=" + teacherName + "]"; } }
配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" default-lazy-init="true" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <!-- 自动检测并注册为spring中的bean+启用注解 --> <context:component-scan base-package="com.lubby.test"></context:component-scan> <bean id="single" class="com.lubby.test.Single" factory-method="getInstance" init-method="init" destroy-method="destroy"> <constructor-arg value="0100102"></constructor-arg> <constructor-arg value="I want to play games...."></constructor-arg> </bean> </beans>
public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("com/lubby/test/test.xml"); Student student = (Student) ctx.getBean("student"); System.out.println(student); } }
常用注解
@Component 表示为Spring组件
@Controller 标示为SpringMVC中的controller
@Service 标示为SpringMVC中的服务
一般用@Componen标示任意自定义注解
@Resource(name = "course")用来注入bean对象 默认是根据name去注入,如果没有指定name那么默认name是类第一个字母小写