IOC操作Bean管理
什么是Bean管理
Bean管理指的是两个操作:Spring创建对象,Spring注入属性
基于xml配置文件方式实现
基于注解方式实现
id属性:给对象取一个名字,作为唯一标识
class属性:通过类全路径(包全路径)创建对象
name属性:给对象取名字,可以重复
测试
会报没找到构造函数的错误
IOC中一种实现,他需要在创建对象的基础上完成
第一步:创建类,定义属性和对应的set方法
第二步:在spring的配置文件中配置对象的创建,配置属性注入
第三步:测试
测试方法
输出结果
第一步:创建类,定义属性,创建属性对应的有参数的构造方法
第二步:在spring的配置文件中进行配置
通过属性名设置
通过下标设置
第三步:测试
测试方法
输出结果
book = com.test.Book@481a15ff
龙王::三人之约
p 名称空间的底层还是set方式注入
Bean管理操作有两种方式
IOC操作Bean管理
基于xml方式创建对象
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
<bean id="user" class="com.test.User"></bean>
</beans>
在spring配置文件中,使用bean标签,标签里面添加对应属性,就可以实现对象创建
在bean标签中有很多属性,介绍常用的属性
创建对象时候,默认也是执行无参数构造方法
package com.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserTest {
@Test
public void testAdd(){
//1 加载spring配置文件
BeanFactory context = new ClassPathXmlApplicationContext("spring.xml");
//获取配置创建对象
User user = context.getBean("user", User.class);
System.out.println("user = " + user);
user.add();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
<bean id="user" class="com.test.User"></bean>
</beans>
package com.test;
public class User {
private String userName;
public User(String userName) {
this.userName = userName;
}
public void add(){
System.out.println("add");
}
}
"D:\IntelliJ IDEA 2021.3.2\JDK\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=64082:D:\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar;D:\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit5-rt.jar;D:\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit-rt.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\charsets.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\access-bridge-64.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\cldrdata.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\dnsns.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\jaccess.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\jfxrt.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\localedata.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\nashorn.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\sunec.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\sunjce_provider.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\sunmscapi.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\sunpkcs11.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\ext\zipfs.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\jce.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\jfr.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\jfxswt.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\jsse.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\management-agent.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\resources.jar;D:\IntelliJ IDEA 2021.3.2\JDK\jre\lib\rt.jar;D:\java\c\target\test-classes;D:\java\c\target\classes;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-beans\5.3.14\spring-beans-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-context\5.3.14\spring-context-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-aop\5.3.14\spring-aop-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-core\5.3.14\spring-core-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-jcl\5.3.14\spring-jcl-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\springframework\spring-expression\5.3.14\spring-expression-5.3.14.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\junit\junit\4.13.2\junit-4.13.2.jar;D:\apache-maven-3.8.4-bin\apache-maven-3.8.4\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.test.UserTest,testAdd
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:671)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.test.UserTest.testAdd(UserTest.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:199)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:333)
... 40 more
进程已结束,退出代码-1
基于xml方式注入属性
DI:依赖注入,就是注入属性
第一种方式 通过set方式注入
package com.test;
/**
* 演示使用set方法进行属性的注入
*/
public class Book {
private String name;
private String author;
public void setAuthor(String author) {
this.author = author;
}
public void setName(String name) {
this.name = name;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
<!--<bean id="user" class="com.test.User"></bean>-->
<!-- 配置对象的创建 -->
<bean id="book" class="com.test.Book">
<!-- set 方法注入属性 -->
<!--
使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
-->
<property name="name" value="斗罗大陆"/>
<property name="author" value="唐家三少"/>
</bean>
</beans>
package com.test;
/**
* 演示使用色图方法进行属性的注入
*/
public class Book {
private String name;
private String author;
public void setAuthor(String author) {
this.author = author;
}
public void setName(String name) {
this.name = name;
}
public void testDemo(){
System.out.println(name + "::" + author);
}
}
package com.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class BookTest {
@Test
public void testBook(){
//1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//2.获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println("book = " + book);
book.testDemo();
}
}
com.test.BookTest,testBook
book = com.test.Book@43a0cee9
斗罗大陆::唐家三少
第二种方式 有参构造器注入
package com.test;
/**
* 使用有参数的构造方法注入
*/
public class Orders {
private String name;
private String address;
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
<!--<bean id="user" class="com.test.User"></bean>-->
<!-- 配置对象的创建 -->
<!--<bean id="book" class="com.test.Book">
set 方法注入属性
使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
<property name="name" value="斗罗大陆"/>
<property name="author" value="唐家三少"/>
</bean>-->
<!-- 有参数构造注入属性 -->
<!-- 配置对象的创建 -->
<bean id="orders" class="com.test.Orders">
<!--
constructor-arg 构造器参数
name:类里面属性名称
value:向属性注入的值
-->
<constructor-arg name="name" value="第一个订单"/>
<constructor-arg name="address" value="中国"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
<!--<bean id="user" class="com.test.User"></bean>-->
<!-- 配置对象的创建 -->
<!--<bean id="book" class="com.test.Book">
set 方法注入属性
使用property完成属性注入
name:类里面属性名称
value:向属性注入的值
<property name="name" value="斗罗大陆"/>
<property name="author" value="唐家三少"/>
</bean>-->
<!-- 有参数构造注入属性 -->
<!-- 配置对象的创建 -->
<bean id="orders" class="com.test.Orders">
<!--
constructor-arg 构造器参数
name:类里面属性名称
value:向属性注入的值
-->
<!--<constructor-arg name="name" value="第一个订单"/>
<constructor-arg name="address" value="中国"/>-->
<constructor-arg index="0" value="第一个订单"/>
<constructor-arg index="1" value="中国"/>
</bean>
</beans>
package com.test;
/**
* 使用有参数的构造方法注入
*/
public class Orders {
private String name;
private String address;
public Orders(String name, String address) {
this.name = name;
this.address = address;
}
public void testOrders(){
System.out.println(name + "::" + address);
}
}
package com.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class OrdersTest {
@Test
public void testOrders() {
//1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//2.获取配置创建的对象
Orders orders = context.getBean("orders", Orders.class);
System.out.println("orders = " + orders);
orders.testOrders();
}
}
orders = com.test.Orders@545997b1
第一个订单::中国
名称空间注入
使用p名称空间注入,可以简化基于xml配置方式
第一步 添加p名称空间在配置文件中
xmlns:p="http://www.springframework.org/schema/p"
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置user对象的创建 -->
第二步 进行属性注入,在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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置对象的创建 -->
<bean id="book" class="com.test.Book" p:name="龙王" p:author="三人之约"/>
</beans>
第三步 测试
package com.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.junit.Assert.*;
public class BookTest {
@Test
public void testBook(){
//1.加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//2.获取配置创建的对象
Book book = context.getBean("book", Book.class);
System.out.println("book = " + book);
book.testDemo();
}
}