一.准备工作
1.创建实体类
package cn.siwen.pojo;
/**
* 手机实体类
*/
public class Phone {
private String Name;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
}
/**
* 人实体类
*/
package cn.siwen.pojo;
public class Person {
private String name;
private int age;
private Phone phone;
public Person(){
System.out.println("调用无参构造方法");
}
public Person(Phone phone) {
super();
this.phone = phone;
System.out.println("调用有参构造方法");
}
public Person(String name, int age,Phone phone) {
super();
this.name = name;
this.age = age;
this.phone = phone;
System.out.println("调用有参构造方法");
}
public void setName(String name) {
this.name = name;
System.out.println("调用了setName方法");
}
public void setAge(int age) {
this.age = age;
System.out.println("调用了setAge方法");
}
public void setPhone(Phone phone) {
this.phone = phone;
System.out.println("调用了setPhone方法");
}
public void printInfo(){
System.out.println(name+"今年"+age+"岁,有一台"+phone.getName());
}
}
2.配置spring容器
记住导入jar包
<?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-3.1.xsd">
</beans>
二.注入方式
1.set注入
<!-- set方法注入-->
<bean id="phone1" class="cn.siwen.pojo.Phone">
<property name="name" value="苹果11 plus" />
</bean>
<bean id="person1" class="cn.siwen.pojo.Person">
<property name="name" value="张三" />
<property name="age">
<value>18</value>
</property>
<property name="phone" ref="phone1" />
</bean>
ref属性值:引用类型
value属性值:八个基本类型+String类型
测试
@org.junit.Test
public void load1(){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Person per=(Person)app.getBean("person1");
per.printInfo();
}
结果
调用无参构造方法
调用了setName方法
调用了setAge方法
调用了setPhone方法
张三今年18岁,有一台苹果11 plus
2.构造方法注入
<!-- 构造方法注入 -->
<bean id="phone2" class="cn.siwen.pojo.Phone">
<property name="name" value="苹果8 plus"></property>
</bean>
<bean id="person2" class="cn.siwen.pojo.Person">
<constructor-arg name="name" index="0" type="String" value="李四"></constructor-arg>
<constructor-arg name="age" index="1" type="int" value="20"></constructor-arg>
<constructor-arg name="phone" index="2" type="cn.siwen.pojo.Phone" ref="phone2"></constructor-arg>
</bean>
name属性值:为构造方法的形参值
index属性值:为参数的下标(参数在哪个位置)
type属性值:为参数的类型
测试
@org.junit.Test
public void load2(){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Person per=(Person)app.getBean("person2");
per.printInfo();
}
结果
调用有参构造方法
李四今年20岁,有一台苹果8 plus
3. p:注入
<!-- p:注入 -->
<bean id="phone3" class="cn.siwen.pojo.Phone" p:name="苹果6 plus"></bean>
<bean id="person3" class="cn.siwen.pojo.Person" p:name="王五" p:age="18" p:phone-ref="phone3"></bean>
p:后面接实体类的属性名
引用类型则还要加ref
测试
@org.junit.Test
public void load3(){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Person per=(Person)app.getBean("person3");
per.printInfo();
}
结果
调用无参构造方法
调用了setAge方法
调用了setName方法
调用了setPhone方法
王五今年18岁,有一台苹果6 plus
4.自动注入
<!-- 自动注入 -->
<bean id="phone" class="cn.siwen.pojo.Phone" p:name="苹果6 plus"></bean>
<bean id="person4" class="cn.siwen.pojo.Person" autowire="byName">
<property name="name" value="张三" />
<property name="age" value="20" />
</bean>
在bean标签中添加autowire属性:
值:
byName:当实体类中的引用类型属性名与容器中已有的id或name属性名一致,则会自动将该对象注入到实体类属性中
byType:当容器中的对象类型与实体类中的属性类型一致,则会自动将该对象注入到实体类属性中
constructor:当容器中的一些对象类型与实体类中构造方法的参数类型一致,则主动注入
测试
@org.junit.Test
public void load4(){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Person per=(Person)app.getBean("person4");
per.printInfo();
}
结果
调用无参构造方法
调用了setName方法
调用了setAge方法
调用了setPhone方法
张三今年20岁,有一台苹果6 plus
5.集合注入
创建实体类
package cn.siwen.pojo;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Conllection {
private List<String> listElement;
private Set<String> setElement;
private Map<String, String> mapElement;
private String[] arrayElement;
public void setListElement(List<String> listElement) {
this.listElement = listElement;
}
public void setSetElement(Set<String> setElement) {
this.setElement = setElement;
}
public void setMapElement(Map<String, String> mapElement) {
this.mapElement = mapElement;
}
public void setArrayElement(String[] arrayElement) {
this.arrayElement = arrayElement;
}
public void printInfo(){
String arrays="";
for(String str:arrayElement){
arrays+=str+",";
}
System.out.println("list:"+listElement);
System.out.println("set:"+setElement);
System.out.println("map:"+mapElement);
System.out.println("array:"+arrays);
}
}
容器配置
<!-- 集合注入 -->
<bean id="conllection" class="cn.siwen.pojo.Conllection">
<property name="listElement">
<list>
<value>轿车1</value>
<value>卡车1</value>
<value>火车1</value>
</list>
</property>
<property name="setElement">
<set>
<value>轿车2</value>
<value>卡车2</value>
<value>火车2</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key>
<value>1</value>
</key>
<value>轿车3</value>
</entry>
<entry>
<key>
<value>2</value>
</key>
<value>卡车3</value>
</entry>
<entry>
<key>
<value>3</value>
</key>
<value>火车3</value>
</entry>
</map>
</property>
<property name="arrayElement">
<array>
<value>轿车4</value>
<value>卡车4</value>
<value>火车4</value>
</array>
</property>
</bean>
测试
@org.junit.Test
public void load5(){
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Conllection con=(Conllection)app.getBean("conllection");
con.printInfo();
}
结果
list:[轿车1, 卡车1, 火车1]
set:[轿车2, 卡车2, 火车2]
map:{1=轿车3, 2=卡车3, 3=火车3}
array:轿车4,卡车4,火车4,
6.子元素<value>与属性value的区别
当值包含特殊字符(“&”,“<”)时:
子元素中可以使用"<![CDATA[-特殊符号-]]>"标记,忽略特殊符号
value属性只能用xml预定义的实体引用来处理 比如:(’&’,’&’)、(’<’,’<’)
7.使用注解
1.添加 spring-context-4.3.9.RELEASE.jar包
2.在头部添加扫描器,指定要扫描的包
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 配置注解扫描器 扫描cn.siwen.pojo包下的所有注解-->
<context:component-scan base-package="cn.siwen.pojo"></context:component-scan>
</beans>
3.在类中使用注解
@Repository("phone")
public class Phone {
}
3.1.其它类级别的注解
@Component("person1") //适用于所有组件
@Repository("person1") //适用于持久层
@Service("person1") //适用于service层
@Controller("person1") //适用于控制层
3.2.指定对象的scope属性(单例或多例)
@Scope(scopeName="singleton") //单例
@Scope(scopeName="prototype") //多利
3.3.注入value值
(1)在私有成员变量中注入
@Value("张三")
private String name;
(2)在set方法中注入
@Value("张三")
public void setName(String name) {
this.name = name;
System.out.println("调用了setName方法");
}
(3)自动注入
@Autowired
private Phone phone;
当容器中有多个相同类型的不同对象,则可以添加@Qualifier注解指定注入对象
@Autowired
@Qualifier("phone2")
private Phone phone;
或者直接用@Resource注解指定注入对象
@Resource("phone2")
private Phone phone;