一、使用注解创建对象的优点
(1)简化配置,不需要在xml文件中配置bean。
(2)使代码更优雅~
二、Spring提供的创建对象的注解
(1)@Component
(2)@Controller
(3)@Service
(4)@Repository
(5)@Configuration
注:虽然注解的名字不一样,但是作用一样,在读取配置文件时,如果开启了组件扫描,就会去对应的包扫描包下的所有类,带有上述注解的类将会被实例化。
(6)@Bean:注解用于方法上,生成的bean的id默认为方法名。对应方法的参数会根据参数类型和bean的类型进行自动装配。(IOC容器为配置类和带有(1)~(5)注解的类中的@Bean创建实例)
三、具体实现(在配置文件中开启组件扫描)
(1)创建类
注:如果注解不设置属性value=xxx,则创建出的bean的id为类名且首字母小写,如果设置了value=xxx,则id为xxx。
@Component
public class Employee {
private Long id;
private String name;
private Department department;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Employee() {
}
public Employee(Long id, String name, Department department) {
this.id = id;
this.name = name;
this.department = department;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", department=" + department +
'}';
}
}
(2)编写配置文件spring-config.xml(需要配置命名空间)
注:属性base-package为要进行组件扫描的包,可以使用逗号分割来指定多个包。
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.wsh.pojo"></context:component-scan>
</beans>
(3)JAVA程序
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Employee employee = context.getBean("employee", Employee.class);
System.out.println(employee.toString());
}
输出
Employee{id=null, name='null', department=null}
四、组件扫描额外配置
(1)组件扫描默认是遇到上述四个注解时均创建bean。
(2)配置组件扫描只在遇到某些注解时才创建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: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">
<context:component-scan base-package="com.wsh.pojo" use-default-filters="false">
<!--在遇到@Controller注解时才创建对象-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
(3)配置组件扫描只在遇到某些注解时不创建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: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">
<context:component-scan base-package="com.wsh.pojo">
<!--在遇到@Controller注解时不创建对象-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
</context:component-scan>
</beans>