欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自动装配

程序员文章站 2022-04-19 22:33:54
...

bean7:配置文件修改后

<?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:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
	<context:annotation-config/>
	<!-- services -->
	<bean id="userDao" class="com.student.annotation.UserDaoImpl"></bean>
	<bean id="userService" class="com.student.annotation.UserServiceImpl" autowire="byName"></bean>
	<bean id="userController" class="com.student.annotation.UserController " autowire="byName"></bean>
</beans>

UserService加上set方法后:

package com.student.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource(name="userDao")
	private UserDao userDao;
	@Override
	public void say() {
		this.userDao.say();
		System.out.println("userService 加载完成!!!");
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	

}

Uservice也加上set方法:

package com.student.annotation;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

@Controller("userController")
public class UserController {
	@Resource(name="userService")
	private UserService userService;
	public void say()
	{
		this.userService.say();
		System.out.println("UserController加载完成");
	}
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}

运行结果:

十月 15, 2019 7:52:02 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]2b71fc7e: startup date [Tue Oct 15 19:52:02 CST 2019]; root of context hierarchy
十月 15, 2019 7:52:02 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [com/student/annotation/bean7.xml]
userDao已经加载加载完成!!
userService 加载完成!!!
UserController加载完成