3、工厂方法创建
(1)静态工厂
package com.pengshiyu.factory; import com.pengshiyu.bean.Person; public class PersonFactory { public static Person newInstance(String name) { return new Person(name); } }
<beans > <bean name="person" class="com.pengshiyu.factory.PersonFactory" factory-method="newInstance"> <constructor-arg name="name" value="Tom" /> </bean> </beans>
(2)动态工厂
package com.pengshiyu.factory; import com.pengshiyu.bean.Person; public class PersonFactory { public Person newInstance(String name) { return new Person(name); } }
<beans> <bean id="factory" class="com.pengshiyu.factory.PersonFactory" /> <bean name="person" factory-bean="factory" factory-method="newInstance"> <constructor-arg name="name" value="Tom"/> </bean> </beans>
意静态工厂 static
四、Spring 配置文件
id 是 bean 的唯一标识符,如果没有配置 id,name 默认为标识符
如果配置了 id,又配置了 name,则 name 是别名
name 可以设置多个别名分隔符可以是空格、逗号、分号
class 是 bean 的全限定名=包名+类名
如果不配置 id 和 name,那么可以可以使用如下方式获取对象
applicationContext.getBean(class)
配置如下
Person person = (Person) context.getBean("person1"); // 或者 Person person = (Person) context.getBean("user"); // 或者 Person person = (Person) context.getBean(Person.class);
获取方式
Person person = (Person) context.getBean("person1"); // 或者 Person person = (Person) context.getBean("user"); // 或者 Person person = (Person) context.getBean(Person.class);
导入文件
<beans> <import resource="person.xml"/> </beans>
五、Spring 依赖注入 DI
dependency injection
依赖:指 bean 对象创建依赖于容器,bean 对象的依赖资源
注入:指 bean 对象依赖的资源由容器来设置和装配
spring 注入
测试
package com.spring.test; import com.pengshiyu.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person) context.getBean("person"); person.sayHello(); } }
构造器注入
package com.pengshiyu.bean; public class Person { private String name; public Person(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name); } }
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <constructor-arg name="name" value="Tom"/> </bean> </beans>
1
2
3
4
5
setter 注入
(1)常量注入
package com.pengshiyu.bean; public class Person { private String name; public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name); } }
<beans > <bean name="person" class="com.pengshiyu.bean.Person"> <property name="name" value="Tom"/> </bean> </beans>
(2)bean 注入
package com.pengshiyu.bean; public class Address { private String address; public void setAddress(String address) { this.address = address; } public String getAddress() { return address; } }
package com.pengshiyu.bean; public class Person { private String name; private Address address; public void setAddress(Address address) { this.address = address; } public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name + this.address.getAddress()); } }
package com.pengshiyu.bean; public class Person { private String name; private Address address; public void setAddress(Address address) { this.address = address; } public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name + this.address.getAddress()); } }
(3)数组注入
package com.pengshiyu.bean; public class Book { private String name; public Book(String name){ this.name = name; } @Override public String toString() { return "《" + this.name + "》"; } }
package com.pengshiyu.bean; import java.util.Arrays; public class Person { private Book[] books; public void setBooks(Book[] books) { this.books = books; } @Override public String toString() { return "Person{" + "books=" + Arrays.toString(books) + '}'; } }
<beans > <bean name="person" class="com.pengshiyu.bean.Person"> <property name="books"> <array> <value>水浒传</value> <value>红楼梦</value> <value>三国演义</value> <value>西游记</value> </array> </property> </bean> </beans>
(4)List 注入
package com.pengshiyu.bean; import java.util.Arrays; import java.util.List; public class Person { private List[] books; public void setBooks(List[] books) { this.books = books; } @Override public String toString() { return "Person{" + "books=" + Arrays.toString(books) + '}'; } }
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <property name="books"> <list> <value>水浒传</value> <value>红楼梦</value> <value>三国演义</value> <value>西游记</value> </list> </property> </bean> </beans>
(5)Map 注入
package com.pengshiyu.bean; import java.util.Map; public class Person { private Map cards; public void setCards(Map cards) { this.cards = cards; } @Override public String toString() { return cards.toString(); } }
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <property name="cards"> <map> <entry key="中国银行" value="123456"></entry> <entry key="建设银行" value="123456"></entry> </map> </property> </bean> </beans>
(6)Set 注入
package com.pengshiyu.bean; import java.util.Set; public class Person { private Set games; public void setGames(Set games) { this.games = games; } @Override public String toString() { return games.toString(); } }
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <property name="games"> <set> <value>英雄联盟</value> <value>王者荣耀</value> </set> </property> </bean> </beans>
(7)null 注入
package com.pengshiyu.bean; public class Person { public void setWife(String wife) { this.wife = wife; } private String wife; @Override public String toString() { return wife; } }
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <property name="wife"><null/></property> </bean> </beans>
(8) Properties 注入
package com.pengshiyu.bean; import java.util.Properties; public class Person { private Properties props; public void setProps(Properties props) { this.props = props; } @Override public String toString() { return this.props.toString(); } }
<beans > <bean name="person" class="com.pengshiyu.bean.Person"> <property name="props"> <props> <prop key="name">Tom</prop> <prop key="sex">Man</prop> </props> </property> </bean> </beans>
(9) p 命名空间注入
需要有对应的 set 方法
package com.pengshiyu.bean; public class Person { private String name; private int age; public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
头文件需要引入
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"> <bean name="person" class="com.pengshiyu.bean.Person" p:name="Tom" p:age="23"/> </beans>
(10)c 命名空间注入
要求有对应的构造方法
package com.pengshiyu.bean; public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
头文件需要引入
xmlns:c="http://www.springframework.org/schema/c"
<?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: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 name="person" class="com.pengshiyu.bean.Person" c:name="Tom" c:age="23"/> </beans>
六、bean 的作用域
spring
桥梁
轻量级
易学
ioc di
app
事务
整合框架
scope:
singleton 单例 整个容器只有一个对象实例(默认)
prototype 原型 每次获取 Bean 都产生一个新对象
request 每次请求时创建一个新的对象
session 会话范围内有一个对象
global session 只在 portlet 下有用,表示 applicatio
application 在应用范围中有一个对象
Bean 自动装配
autowire
byName
byType
constructor
不推荐使用自动装配