spring注入成员对象

就是将对象注入到另外一个对象中。这个例子就是有一个学校类,学校类中有一个校长类,最后使用测试文件输出学校类中的信息。

代码结构



学校类

package com.test.SpringGetSet;

public class School {
	private Present present;
	
	public Present getPresent() {
		return present;
	}

	public void setPresent(Present present) {
		this.present = present;
	}

	public String getShoolName() {
		return shoolName;
	}

	public void setShoolName(String shoolName) {
		this.shoolName = shoolName;
	}

	String shoolName;
	
	public void say()
	{
		System.out.println("校名:"+shoolName);
		System.out.println("校长姓名:"+present.getName());
	}

}

校长类

package com.test.SpringGetSet;

public class Present {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	private int age;

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="present" class="com.test.SpringGetSet.Present">
        <property name="name" value="杨发癫" />
        <property name="age" value="40" />
    </bean>
    <bean id="school" class="com.test.SpringGetSet.School">
        <property name="shoolName" value="上饶县中" />
        <property name="present">
            <ref bean="present"/>
        </property>
    </bean>
</beans>


测试类

package com.test.SpringGetSet;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import SpringByHand.Hello;

public class SetDemo001 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/test/SpringGetSet/beans.xml"));
		School s=(School) factory.getBean("school");
		s.say();
	}

}


输出结果



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,我们可以使用依赖注入(dependency injection)来注入Mapper对象。首先,确保你已经配置了MyBatis或者其他ORM框架,并且已经创建了Mapper接口和对应的Mapper实现类。 接下来,你可以在Spring的配置文件(如applicationContext.xml)中使用`<bean>`标签来定义Mapper对象注入。例如,假设我们有一个名为`UserMapper`的Mapper接口,可以按照以下方式进行注入: ```xml <!-- 配置Mapper接口 --> <bean id="userMapper" class="com.example.UserMapper"/> <!-- 配置Service层 --> <bean id="userService" class="com.example.UserService"> <!-- 注入Mapper对象 --> <property name="userMapper" ref="userMapper"/> </bean> ``` 在上述示例中,我们先定义了一个名为`userMapper`的Bean,并指定了对应的实现类`com.example.UserMapper`。然后,我们在Service层的Bean定义中使用`<property>`标签注入了`userMapper`对象。 接下来,在你的Service类中,你可以使用`@Autowired`注解将Mapper对象自动注入成员变量中。例如: ```java @Service public class UserService { @Autowired private UserMapper userMapper; // ... } ``` 现在,你就可以在Service类中使用`userMapper`对象了。Spring会自动为你创建并注入`userMapper`实例。 需要注意的是,为了使依赖注入正常工作,确保你已经正确配置了Spring和MyBatis(或其他ORM框架),并且在Spring配置文件中正确地定义了Mapper对象和其他相关的Bean。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值