创建maven工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maliqiang</groupId>
<artifactId>spring-ioc-test01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
创建实体类
public class User {
private Integer userid;
private String username;
private String password;
public User() {
System.out.println("NoArgConstructor");
}
public User(Integer userid, String username, String password) {
System.out.println("AllArgConstructor");
this.userid = userid;
this.username = username;
this.password = password;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
System.out.println("setUserid");
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
System.out.println("setUsername");
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
System.out.println("setPassword");
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userid=" + userid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
public class Student {
private String[] books;
private List<String> bookList;
private Set<String> bookSet;
private Map<String, String> bookMap;
private List<Book> bookList2;
public Student() {
}
public Student(String[] books, List<String> bookList, Set<String> bookSet, Map<String, String> bookMap, List<Book> bookList2) {
this.books = books;
this.bookList = bookList;
this.bookSet = bookSet;
this.bookMap = bookMap;
this.bookList2 = bookList2;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getBookList() {
return bookList;
}
public void setBookList(List<String> bookList) {
this.bookList = bookList;
}
public Set<String> getBookSet() {
return bookSet;
}
public void setBookSet(Set<String> bookSet) {
this.bookSet = bookSet;
}
public Map<String, String> getBookMap() {
return bookMap;
}
public void setBookMap(Map<String, String> bookMap) {
this.bookMap = bookMap;
}
public List<Book> getBookList2() {
return bookList2;
}
public void setBookList2(List<Book> bookList2) {
this.bookList2 = bookList2;
}
@Override
public String toString() {
return "Student{" +
"books=" + Arrays.toString(books) +
", bookList=" + bookList +
", bookSet=" + bookSet +
", bookMap=" + bookMap +
", bookList2=" + bookList2 +
'}';
}
}
public class Mouse {
private String name;
private Date birthdate;
public Mouse() {
}
public Mouse(String name, Date birthdate) {
this.name = name;
this.birthdate = birthdate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthdate() {
return birthdate;
}
public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}
@Override
public String toString() {
return "Mouse{" +
"name='" + name + '\'' +
", birthdate=" + birthdate +
'}';
}
}
public class Cat {
private String name;
private Mouse mouse1;
public Cat() {
}
public Cat(String name, Mouse mouse1) {
this.name = name;
this.mouse1 = mouse1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Mouse getMouse1() {
return mouse1;
}
public void setMouse1(Mouse mouse1) {
this.mouse1 = mouse1;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", mouse1=" + mouse1 +
'}';
}
}
public class Book {
private String bname;
private String author;
public Book() {
}
public Book(String bname, String author) {
this.bname = bname;
this.author = author;
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", author='" + author + '\'' +
'}';
}
}
public class BookFactory implements FactoryBean<Book> {
public Book getObject() throws Exception {
Book book = new Book();
book.setBname("JAVA");
book.setAuthor("小灰灰");
return book;
}
public Class<?> getObjectType() {
return null;
}
}
创建applicationContext.xml文件,通过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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
lazy-init(懒加载 调用的时候 才会加载) 默认为 false
scope(默认为单例模式 singleton)
-->
<bean id="user1" class="com.maliqiang.bean.User"></bean>
<!--
通过 set 方式 给对象属性赋值
-->
<bean id="user2" class="com.maliqiang.bean.User">
<property name="userid" value="1"></property>
<property name="username" value="张三"></property>
<property name="password" value="abcdef"></property>
</bean>
<!--
通过构造方法给属性赋值
还可以通过index 给属性赋值
-->
<bean id="user3" class="com.maliqiang.bean.User">
<constructor-arg name="userid" value="2"></constructor-arg>
<constructor-arg name="username" value="小明"></constructor-arg>
<constructor-arg name="password" value="def"></constructor-arg>
</bean>
<bean id="user4" class="com.maliqiang.bean.User">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="小黑"></constructor-arg>
<constructor-arg index="2" value="dsafd"></constructor-arg>
</bean>
<!--
通过 p 名称空间 和 c 名称空间给对象属性赋值
-->
<bean id="user5" class="com.maliqiang.bean.User" p:userid="4" p:username="小东" p:password="111111"></bean>
<bean id="user6" class="com.maliqiang.bean.User" c:userid="5" c:username="小西" c:password="222222"></bean>
</beans>
创建测试类,测试
public class Test01 {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void testGetBean1() {
User user1 = applicationContext.getBean("user1", User.class);
System.out.println("user1 = " + user1);
User user2 = applicationContext.getBean("user1", User.class);
System.out.println("user1==user2 = " + (user1 == user2));
}
@Test
public void testGetBean2() {
User user2 = applicationContext.getBean("user2", User.class);
System.out.println("user2 = " + user2);
}
@Test
public void testGetBean3() {
User user3 = applicationContext.getBean("user3", User.class);
System.out.println("user3 = " + user3);
}
@Test
public void testGetBean4() {
User user4 = applicationContext.getBean("user4", User.class);
System.out.println("user4 = " + user4);
}
@Test
public void testGetBean5() {
User user5 = applicationContext.getBean("user5", User.class);
System.out.println("user5 = " + user5);
User user6 = applicationContext.getBean("user6", User.class);
System.out.println("user6 = " + user6);
}
}
关于给属性赋值为null 和 特殊符号
<?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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user1" class="com.maliqiang.bean.User">
<!-- <property name="userid" value="null"></property>-->
<!-- 引用类型 赋值 null -->
<property name="userid">
<null></null>
</property>
<!-- 特殊符号 &(&) <(<) >(>) -->
<property name="username" value="&123<>"></property>
<!-- 特殊符号 <![CDATA[内容]]>-->
<property name="password">
<value><![CDATA[&123<>]]></value>
</property>
</bean>
</beans>
public class Test02 {
@Test
public void testGetBean1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext2.xml");
User user1 = applicationContext.getBean("user1", User.class);
System.out.println("user1 = " + user1);
}
}
引入外部,内部和级联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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 告诉容器 准备一个Date对象-->
<bean id="date" class="java.util.Date"></bean>
<bean id="mouse1" class="com.maliqiang.bean.Mouse">
<property name="name" value="Jerry"></property>
<!-- <property name="birthdate" value="2022-02-02"></property>-->
<!-- bean 引用 引用外部bean-->
<property name="birthdate" ref="date"></property>
</bean>
<bean id="cat" class="com.maliqiang.bean.Cat">
<property name="name" value="Tom"></property>
<!--引入外部的bean-->
<property name="mouse1" ref="mouse1"></property>
</bean>
<bean id="cat2" class="com.maliqiang.bean.Cat">
<property name="name" value="Tom"></property>
<!--引入内部的bean-->
<property name="mouse1">
<bean id="mouse2" class="com.maliqiang.bean.Mouse">
<property name="name" value="Jerry2"></property>
<property name="birthdate">
<bean id="date2" class="java.util.Date"></bean>
</property>
</bean>
</property>
</bean>
<bean id="mouse3" class="com.maliqiang.bean.Mouse"></bean>
<bean id="cat3" class="com.maliqiang.bean.Cat">
<property name="name" value="Tom2"></property>
<!-- 级联bean -->
<property name="mouse1" ref="mouse3">
</property>
<property name="mouse1.name" value="Jerry3"></property>
<property name="mouse1.birthdate">
<bean class="java.util.Date"></bean>
</property>
</bean>
</beans>
public class Test03 {
@Test
public void testGetBean1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext3.xml");
Mouse mouse1 = applicationContext.getBean("mouse1", Mouse.class);
System.out.println("mouse1 = " + mouse1);
Cat cat = applicationContext.getBean("cat", Cat.class);
System.out.println("cat = " + cat);
Cat cat2 = applicationContext.getBean("cat2", Cat.class);
System.out.println("cat2 = " + cat2);
Cat cat3 = applicationContext.getBean("cat3", Cat.class);
System.out.println("cat3 = " + cat3);
}
}
给属性赋值为 数组、list集合、set 集合、map 集合,对象list,定义共享数据
<?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:c="http://www.springframework.org/schema/c"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 定义共享数据 -->
<util:list id="outerBookList">
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="JAVA" p:author="小灰灰"></bean>
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="MySQL" p:author="小灰灰"></bean>
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="Spring" p:author="小灰灰"></bean>
</util:list>
<bean id="student" class="com.maliqiang.bean.Student">
<!--数组-->
<property name="books">
<array>
<value>JAVA</value>
<value>MySQL</value>
<value>Spring</value>
</array>
</property>
<!-- list集合-->
<property name="bookList">
<list>
<value>JAVA</value>
<value>MySQL</value>
<value>Spring</value>
</list>
</property>
<!-- set 集合 -->
<property name="bookSet">
<set>
<value>JAVA</value>
<value>MySQL</value>
<value>Spring</value>
</set>
</property>
<!-- map 集合-->
<property name="bookMap">
<map>
<entry key="JAVA" value="小灰灰"></entry>
<entry key="MySQL" value="小灰灰"></entry>
<entry key="Spring" value="小灰灰"></entry>
</map>
</property>
<!-- 对象list -->
<!-- <property name="bookList2">
<list>
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="JAVA" p:author="小灰灰"></bean>
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="MySQL" p:author="小灰灰"></bean>
<bean id="b1" class="com.maliqiang.bean.Book" p:bname="Spring" p:author="小灰灰"></bean>
</list>
</property>-->
<property name="bookList2" ref="outerBookList">
</property>
</bean>
</beans>
public class Test04 {
@Test
public void testGetBean1() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext4.xml");
Student student = applicationContext.getBean("student", Student.class);
System.out.println("student.getBooks() = " + Arrays.toString(student.getBooks()));
System.out.println("student.getBookList() = " + student.getBookList());
System.out.println("student.getBookSet() = " + student.getBookSet());
System.out.println("student.getBookMap() = " + student.getBookMap());
System.out.println("student.getBookList2() = " + student.getBookList2());
}
}