1 使用有参构造注入属性
public class User {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
User()
{
}
public void sop()
{
System.out.println(userName);
}
public void add()
{
System.out.println("add.............");
}
public static void main(String[] args)
{
User user = new User();
user.add();
}
}
// 配置文件,其中name属性值是User类中的属性字段名称
<bean id = "bean4" class="com.grl.ioc.User">
<constructor-arg name = "userName" value="小胖子"></constructor-arg>
</bean>
// 测试代码:
public void Text()
{
//加载核心配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//得到配置创建对象
UserService user = (UserService)context.getBean("bean4");
user.sop();
}
2 使用set方法
public class User {
private String userName;
public void setUserName(String userName) {
this.userName = userName;
}
User()
{
}
public void sop()
{
System.out.println(userName);
}
}
// 配置文件,其中name属性值是User
<bean id = "bean5" class = "com.grl.ioc.User">
<property name="userName" value = "小胖子"></property>
</bean>
// 测试代码
public void Text()
{
//加载核心配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//得到配置创建对象
UserService user = (UserService)context.getBean("bean5");
user.sop();
}
3 注入复杂类型,例如参数是对象
public class UserService {
private User user;
public void setUser(User user) {
this.user = user;
}
public void sop()
{
System.out.println("hahah 终于成功了!");
}
}
//配置文件
<bean id="user" class="com.grl.ioc.User"></bean>
<bean id = "UserService" class = "com.grl.arg.UserService">
<property name="user" ref = "user"></property>
</bean>
// 测试代码
public class canshuTest {
@Test
public void Text()
{
//加载核心配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//得到配置创建对象
UserService user = (UserService)context.getBean("UserService");
user.sop();
}
}
4 复杂类型的注入
public class Person {
private String[] arr;
private List<String> list;
private Map<String,String> map;
private Properties properties;
public void setArr(String[] arr) {
this.arr = arr;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void sop()
{
System.out.println(arr);
System.out.println(list);
System.out.println(map);
System.out.println(properties);
}
}
// 配置文件
<bean id = "person" class = "com.grl.arg.Person">
<property name="arr">
<list>
<value>小马</value>
<value>小王</value>
<value>小金</value>
</list>
</property>
<property name="list">
<list>
<value>好吗</value>
<value>好的</value>
<value>阿弥陀佛</value>
</list>
</property>
<property name="map">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="tom"></entry>
<entry key="cc" value="lili"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="Drivers">com.mysql.jdbc.Driver</prop>
<prop key="User">root</prop>
<prop key="Pwd">123456</prop>
</props>
</property>
</bean>
// 测试代码
@Test
public void Text()
{
//加载核心配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//得到配置创建对象
Person p = (Person)context.getBean("person");
p.sop();
}