1.setter方法注入
1.编写一个测试类, 注意对应属性一定要有setter方法,不然报错
package com.limi.test;
public class User {
private String name;
private Integer age;
private String hoby;
public User(){
}
public User(String name, Integer age){
this.name = name;
this.age = age;
}
public void say(){
System.out.println("name="+name+" age="+age+" hoby="+hoby);
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setHoby(String hoby) {
this.hoby = hoby;
}
}
2.在bean的xml文件中使用setter注入
<?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">
<!--id是唯一标识, class是全类名 -->
<bean id="User" class="com.limi.test.User">
<!-- set方法注入属性,类里面一定要有setter,不然报错-->
<property name="name" value="andy"></property>
<property name="age" value="10"></property>
</bean>
</beans>
3.测试
package com.limi.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test1(){
//1.加载bean的xml文件, 以src为根目录
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
User user = context.getBean("User", User.class);
//3.使用User对象
user.say();
}
}
2.有参构造方法注入
1.在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">
<!--id是唯一标识, class是全类名 -->
<bean id="User" class="com.limi.test.User">
<!-- 使用有参构造注入属性-->
<constructor-arg name="name" value="bob"></constructor-arg>
<!--也可以使用这种方式,index属性表示构造方法里的第几个参数(从0开始)-->
<constructor-arg index="1" value="99"></constructor-arg>
</bean>
</beans>
2.测试, 测试类和上面相同
3.p 名称空间注入(用的少)
1.使用 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">
<!--上面加入个xmlns:p="http://www.springframework.org/schema/p"
添加 p 名称空间在配置文件中, 注意只能写p, 别的不行
-->
<!--id是唯一标识, class是全类名-->
<bean id="User" class="com.limi.test.User" p:name="lo" p:age="88" p:hoby="ball"></bean>
</beans>
2.测试, 测试类和上面相同
4.特殊值的注入
<?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">
<!--id是唯一标识, class是全类名 -->
<bean id="User" class="com.limi.test.User">
<!--特殊字符的注入不能直接书写,需把内容写<![CDATA[]]>里面-->
<property name="name">
<value><![CDATA[\n\n]]></value>
</property>
<property name="hoby">
<value><![CDATA[<<basketball>>]]></value>
</property>
<!--空值, 使用<null>标签-->
<property name="age">
<null></null>
</property>
</bean>
</beans>
2.测试, 测试类和上面相同