spring入门-------按名称自动装配

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ThinkPet/article/details/81870309

1.实体类User.java

package entity;

public class User {

	private String name;
	private Integer age;
	private String sex;
	//setter注入
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	
}

2.实体类PrintInfo.java

package entity;

public class PrintInfo {
	//注入User对象
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    //打印的User对象中的属性
    public void PrintUser(){
        System.out.println("PrintInfo打印的User属性");
        System.out.println("----------------------");
        System.out.println("用户姓名--"+user.getName());//输出用户的姓名
        System.out.println("用户年龄--"+user.getAge());//输出用户的年龄
        System.out.println("用户性别--"+user.getSex());//输出用户的性别
    }
}

3.测试类Test.java

package main;

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

import entity.PrintInfo;


public class Test {

	public static void main(String[] args) {
		// 导入src下的applicationContext.xml配置文件
		Resource resource=new ClassPathResource("applicationContext.xml");
		//读取配置文件
		BeanFactory factory=new XmlBeanFactory(resource);
		//获取bean
		PrintInfo printInfo = (PrintInfo)factory.getBean("printInfo");
		
		printInfo.PrintUser();
	}

}

4.配置文件web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Test820</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

5.配置文件applicationContext.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-2.5.xsd">
	 
	<bean id="user" class="entity.User">
		<property name="name">
			<value>小强</value>
		</property>
		<property name="age">
			<value>26</value>
		</property>
		<property name="sex">
			<value>男</value>
		</property>
	</bean>
	<!-- PrintInfo中已经注入user对象,可以按bean名自动装配 -->
	<bean autowire="byName" id="printInfo" class="entity.PrintInfo" />
	
	
</beans>

6.运行效果

猜你喜欢

转载自blog.csdn.net/ThinkPet/article/details/81870309