Spring 参数注入方法

本文详细介绍了Spring中四种属性注入的方式:1. 使用有参构造注入;2. 使用set方法注入;3. 注入复杂类型,如对象;4. 注入数组、列表、映射及属性对象。通过实例代码展示了如何在配置文件中设置注入属性,并在测试代码中验证注入效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值