Spring装配、注解装配

目录

列:装配的两种方式

创建po对象Student1.java、Student2.java、Students.java

测试

注解装配

创建po层User.java

创建dao层UserDao.java、UserDaoImp.java

创建service层UserService.java、UserServiceImp.java

创建Controller层UserController.java

bean.xml

测试


列:装配的两种方式

创建po对象Student1.java、Student2.java、Students.java

 

<?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.xsd">

	<bean id="student1" class="com.lws.test.Student1"></bean>
	<bean id="student2" class="com.lws.test.Student2"></bean>

	<!-- 1装配:根据名称装配   byName:对象的属性名与实例化的Bean的名称要一致  (默认是按名称装配) -->	
	<bean id="students" class="com.lws.test.Students" autowire="byName"></bean>

	<!-- 1.2 spring装配 按类型装配    byType:对象的属性名的类型与实例化的Bean的Class类型要一致  (默认是按名称装配)  -->
    <!-- <bean id="beans" class="com.iotek.spring.test6.Beans" autowire="byType"></bean> -->
    
</beans>

测试

public class Test {
	private ClassPathXmlApplicationContext context;
	@Before
	public void init() {
		context=new ClassPathXmlApplicationContext("beans.xml");
	}
	@org.junit.Test
	public void students() {
		Students students=(Students)context.getBean("students");
		System.out.println(students);
	}
}

注解装配

创建po层User.java

创建dao层UserDao.java、UserDaoImp.java

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

//@Component(value="userDaoImp")//注解的标记
@Repository(value="userDaoImp")//用于对dao层进行标注
public class UserDaoImp implements UserDao{

	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("userdaoImp层实现类");
	}
}

创建service层UserService.java、UserServiceImp.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

//@Component(value="userServiceImp")
@Service(value="userServiceImp")//service层注解
public class UserServiceImp implements UserService {
	private UserDao userDao;
	
	@Autowired// 自动装配默认使用类型注入.
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public void register() {
		// TODO Auto-generated method stub
		userDao.save();
		System.out.println("userService层实现类--register");
	}
}

创建Controller层UserController.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Controller(value="userController")
public class UserController {
	@Autowired
	@Qualifier("userServiceImp")//按名称进行注入.@Service(value="userServiceImp")、可以不写
	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public void a() {
		System.out.println("UserController");
		userService.register();
	}
}

bean.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	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">

	<!--2注解装配 命名空间  xmlns:context="http://www.springframework.org/schema/context"
	     约束http://www.springframework.org/schema/context 
		  http://www.springframework.org/schema/context/spring-context.xsd" -->
		<!-- 扫描包下的所有的类 -->
		<context:component-scan base-package="com.lws2"></context:component-scan>
</beans>

测试

	private ClassPathXmlApplicationContext context;
	@Before
	public void init() {
		context=new ClassPathXmlApplicationContext("beans2.xml");
	}
	//测试userDaoImp类是否实例化
	@org.junit.Test
	public void test1() {
		 UserDao userDao=(UserDao) context.getBean("userDaoImp");
	        userDao.save();
	}
	//测试userServiceImp类是否实例化
	@org.junit.Test
	public void test2() {
		 UserService aa=(UserService) context.getBean("userServiceImp");
	        aa.register();
	}
	//测试UserController类是否实例化
	@org.junit.Test
	public void test3() {
		 UserController aa=(UserController) context.getBean("userController");
	        aa.a();
	}

结果:

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章