类目录结构
public class Cpu {
private Integer num;
private String cpuName;
private String factory;
public Cpu() {
}
public Cpu(Integer num, String cpuName, String factory) {
this.num = num;
this.cpuName = cpuName;
this.factory = factory;
}
public void setNum(Integer num) {
this.num = num;
}
public void setCpuName(String cpuName) {
this.cpuName = cpuName;
}
public void setFactory(String factory) {
this.factory = factory;
}
@Override
public String toString() {
return "Cpu{" +
"num=" + num +
", cpuName='" + cpuName + '\'' +
", factory='" + factory + '\'' +
'}';
}
}
public class Computer {
private Integer num;
private String computerName;
private Cpu cpu;
public Computer() {
}
public Computer(Integer num, String computerName, Cpu cpu) {
this.num = num;
this.computerName = computerName;
this.cpu = cpu;
}
public void setNum(Integer num) {
this.num = num;
}
public void setComputerName(String computerName) {
this.computerName = computerName;
}
public void setCpu(Cpu cpu) {
this.cpu = cpu;
}
@Override
public String toString() {
return "Computer{" +
"num=" + num +
", computerName='" + computerName + '\'' +
", cpu=" + cpu +
'}';
}
}
public class SuperComputer {
private String[] computerNames;
private List<String> factorys;
private Set<Cpu> cpus;
private Map<Integer,String> users;
private Properties userGirls;
public void setComputerNames(String[] computerNames) {
this.computerNames = computerNames;
}
public void setFactorys(List<String> factorys) {
this.factorys = factorys;
}
public void setCpus(Set<Cpu> cpus) {
this.cpus = cpus;
}
public void setUsers(Map<Integer, String> users) {
this.users = users;
}
public void setUserGirls(Properties userGirls) {
this.userGirls = userGirls;
}
@Override
public String toString() {
return "SuperComputer{" +
"computerNames=" + Arrays.toString(computerNames) +
", factorys=" + factorys +
", cpus=" + cpus +
", users=" + users +
", userGirls=" + userGirls +
'}';
}
}
<?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
https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
1. 使用无参构造创建对象
在xml文件中加入
<bean id="cpu" class="org.example.IOC.Cpu"></bean>
@Test
public void cpuTest(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
Cpu bean1 = (Cpu) context.getBean("cpu");
System.out.println(bean1);
}
2. 使用有参构造创建对象
<bean id="cpu" class="org.example.IOC.Cpu">
<!-- 通过类型定位属性-->
<constructor-arg type="java.lang.Integer" value="123"></constructor-arg>
<!--通过下标定位属性-->
<constructor-arg index="1" value="Inter"></constructor-arg>
<!--通过属性名定位属性-->
<constructor-arg name="factory" value="InterFactory"></constructor-arg>
</bean>
@Test
public void cpuTest(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
Cpu bean1 = (Cpu) context.getBean("cpu");
System.out.println(bean1);
}
3. 带有类类型属性的构造器创建对象
@Test
public void cpuTest(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
Computer computer = (Computer) context.getBean("computer");
System.out.println(computer);
}
<bean id="cpu" class="org.example.IOC.Cpu">
<!-- 通过类型定位属性-->
<constructor-arg type="java.lang.Integer" value="123"></constructor-arg>
<!--通过下标定位属性-->
<constructor-arg index="1" value="Inter"></constructor-arg>
<!--通过属性名定位属性-->
<constructor-arg name="factory" value="InterFactory"></constructor-arg>
</bean>
<!--构造器方法赋值-->
<bean id="computer" class="org.example.IOC.Computer">
<constructor-arg name="num" value="100"></constructor-arg>
<constructor-arg name="computerName" value="外星人"></constructor-arg>
<constructor-arg name="cpu" ref="cpu"></constructor-arg>
</bean>
<bean id="computer" class="org.example.IOC.Computer">
<constructor-arg name="num" value="100"></constructor-arg>
<constructor-arg name="computerName" value="外星人"></constructor-arg>
<constructor-arg name="cpu">
<bean class="org.example.IOC.Cpu">
<constructor-arg type="java.lang.Integer" value="666"></constructor-arg>
<constructor-arg index="1" value="Inter"></constructor-arg>
<constructor-arg name="factory" value="InterFactory"></constructor-arg>
</bean>
</constructor-arg>
</bean>
4. 使用Set方法传递参数
@Test
public void cpuTest(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
Computer computer1 = (Computer) context.getBean("computer1");
System.out.println(computer1);
}
<bean id="cpu" class="org.example.IOC.Cpu">
<!-- 通过类型定位属性-->
<constructor-arg type="java.lang.Integer" value="123"></constructor-arg>
<!--通过下标定位属性-->
<constructor-arg index="1" value="Inter"></constructor-arg>
<!--通过属性名定位属性-->
<constructor-arg name="factory" value="InterFactory"></constructor-arg>
</bean>
<!--Set方法传参数-->
<bean id="computer1" class="org.example.IOC.Computer">
<!--name: 指定set方法 value: 传入的值-->
<property name="num" value="200"></property>
<property name="computerName" value="苹果"></property>
<!--ref: 引用spring容器中的对象-->
<property name="cpu" ref="cpu"></property>
</bean>
5. 集合, 数组的参数传递
@Test
public void cpuTest(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
SuperComputer superComputer = (SuperComputer) context.getBean("superComputer");
System.out.println(superComputer);
}
<bean id="superComputer" class="org.example.IOC.SuperComputer">
<property name="computerNames">
<array>
<value>MacPro</value>
<value>外星人</value>
</array>
</property>
<property name="factorys">
<list>
<value>因特尔工厂</value>
<value>苹果工厂</value>
</list>
</property>
<property name="cpus">
<set>
<bean class="org.example.IOC.Cpu">
<constructor-arg name="num" value="111"></constructor-arg>
<constructor-arg name="cpuName" value="Inter"></constructor-arg>
<constructor-arg name="factory" value="因特尔工厂"></constructor-arg>
</bean>
<bean class="org.example.IOC.Cpu">
<constructor-arg name="num" value="222"></constructor-arg>
<constructor-arg name="cpuName" value="苹果"></constructor-arg>
<constructor-arg name="factory" value="苹果工厂"></constructor-arg>
</bean>
</set>
</property>
<property name="users">
<map>
<entry key="1" value="张三"></entry>
<entry key="2" value="李四"></entry>
</map>
</property>
<property name="userGirls">
<props>
<prop key="1">貂蝉</prop>
<prop key="2">西施</prop>
</props>
</property>
</bean>
6. 注解的使用
<!--扫描整个包-->
<context:component-scan base-package="org.example.IOC"></context:component-scan>
6.1 类注解的使用
**@Component @Service 业务层 @Controller 表现层 @Repository 持久层**
//@Component(value = "cpu")
@Component //默认id: 类名首字母小写
//@Service(value = "cpu")
//@Service
//@Controller(value = "cpu")
//@Controller
//@Repository(value = "cpu")
//@Repository
public class Cpu {
private Integer num;
private String cpuName;
private String factory;
//...此处省略(与上方对应类代码相同)
}
@Test
public void Test02(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
//注解的使用
Cpu bean = (Cpu) context.getBean("cpu");
System.out.println(bean);
}
6.1 @Value注解的使用
@Component //默认id: 类名首字母小写
public class Cpu {
@Value("666")
private Integer num;
@Value("因特尔")
private String cpuName;
@Value("InterFactory")
private String factory;
//....此处省略(与上面代码一样)
}
6.2 类类型属性注解的使用
@Autowired @Qualifier(value = "cpu") @Resource(name = "cpu")
@Component
public class Computer {
private Integer num;
private String computerName;
@Autowired
@Qualifier(value = "cpu")
//@Resource(name = "cpu")
private Cpu cpu;
//...此处省略(与上方对应类代码相同)
}
6.3 单例多例注解的使用
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON) //单例
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) //多例
public class Computer {
......
}
6.4 @PostConstruct和@PreDestroy注解的使用
@Component
public class Computer {
private Integer num;
private String computerName;
@Autowired
@Qualifier(value = "cpu")
private Cpu cpu;
//...此处省略(与上方对应类代码相同)
//创建容器时调用
@PostConstruct
public void initMethod(){
System.out.println("容器被创建!");
}
//销毁容器时调用
@PreDestroy
public void destroyMethod(){
System.out.println("容器被销毁!");
}
}
@Test
public void Test02(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
((ClassPathXmlApplicationContext)context).close();
}
单例模式下:
6.5 通过字节码获取对象
@Test
public void Test02(){
ApplicationContext context =
new ClassPathXmlApplicationContext("spring_bean.xml");
//通过字节码拿到对象
//缺点: 当配置了多个bean时, 因为字节码是相同的,因此会造成混淆(不知道给你返回哪一个bean)
Cpu bean = context.getBean(Cpu.class);
System.out.println(bean);
}