1,分别创建Bean1,Bean2,Bean3类
package com.entor.entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @Component 注解作用在类上,作用:相当于配置文件中一个bean标签,默认的id属性名称是该类名的首字母小写格式,也可以指定名称
* @Controller
* @Service
* @Repository
* 以上四个注解都是用在类上面,作用类似,主要区别是用来标识不同类型的对象,
* @Controller 注解用来标识处理客户端请求类
* @Service 注解用来标识业务处理类
* @Repository 注解用来标识数据访问层处理类
* @Component 注解用来标识除了上面三个标识以外其他组件
*/
@Component("bean1")
public class Bean1 {
/**
* @Resource 注解用在属性上面,作用:根据属性名称去配置文件中获取对应id名称的bean对象,找到之后赋值给该属性,
* 如果没有找到,则根据属性类型去配置文件查找,如果找到多个则报错,使用注解后可以省略属性的get/set方法
*
* @Autowired 注解用在属性上面,作用:根据属性类型去配置文件查找对象,如果找到多个,再根据属性名称去查找,如果找不到则报错
* 可以配合@Qualifier注解使用,指定名称去查找,如果找不到则报错,不会再根据属性名称去查找
*/
@Autowired
private Bean2 bean2;
@Resource
private Bean3 bean3;
@Override
public String toString() {
return "Bean1{" +
"bean2=" + bean2 +
", bean3=" + bean3 +
'}';
}
}
package com.entor.entity;
import org.springframework.stereotype.Controller;
@Controller
public class Bean2 {
private int id;
private String name;
private Bean3 bean3;
@Override
public String toString() {
return "Bean2{" +
"id=" + id +
", name='" + name + '\'' +
", bean3=" + bean3 +
'}';
}
}
package com.entor.entity;
import org.springframework.stereotype.Controller;
@Controller
public class Bean3 {
private int id;
private String name;
private String password;
@Override
public String toString() {
return "Bean3{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
2,使用注解模式
三种属性注解:
@Autowired :根据属性名称去配置文件中获取对应的id名称的bean对象,找到后赋值给该属性如果没有找到,则根据属性类型class去配置文件查找,如果找到多个则报错,使用注解后可以省略属性的get/set方法
@Resource
注解用在属性上面,作用:根据属性类型去配置文件查找对象,如果找到多个,再根据属性名称去查找,如果找不到则报错
@Qualifier:注解使用,指定名称去查找,如果找不到则报错,不会再根据属性名称去查找
3.2,4种类注解,作用:相当于一个bean标签,默认id属性名称是该类名的首字母小写格式
@Controller 注解用来标识处理客户端请求类 @Service 注解用来标识业务处理类 @Repository 注解用来标识数据访问层处理类 @Component 注解用来标识除了上面三个标识以外其他组件
4,allcationContext资源文件
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- <!–如果使用xml配置方法,需要写set,get方法才可使用–>
<bean id="bean1" class="com.entor.entity.Bean1">
<property name="bean2" ref="bean2"/>
<property name="bean3" ref="bean3"/>
</bean>
<bean id="bean2" class="com.entor.entity.Bean2">
<property name="id" value="1"/>
<property name="name" value="张三"/>
<property name="bean3" ref="bean3"/>
</bean>
<bean id="bean3" class="com.entor.entity.Bean3">
<property name="id" value="2"/>
<property name="name" value="李四"/>
<property name="age" value="20"/>
</bean>-->
<!--注解模式-->
<!--开启注解模式-->
<context:annotation-config/>
<!--配置组件包扫描指定基本的包名称,自动扫描该包以及子包下的所有类,
查找类上有注解@Component,@Controller,@Service,@Repository自动创建对象-->
<context:component-scan base-package="com.entor.entity"/>
</beans>
5,测试类
package com.entor.text;
import com.entor.entity.Bean1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test2 {
public static void main(String[] args) {
//加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext-bean.xml");
Bean1 bean1 = context.getBean(Bean1.class);
System.out.println(bean1);
}
}