Spring 입문 블 로그 [5. Spring 용기 구성 주해 @ Component 와 Resouces 완전 주해 설정 실현]

@Resource @Component   XML  


1、@Resource   :
@Resource J2EE   。               。     name     name   。     field  setter  
public class UserAction {
	private UserService userService;
	
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public void addUser(){
		userService.HelloWorld();
	}
}


2、@Component
@Component <context:component-scan base-package="com.spring"></context:component-scan>     XML  ,                  。

    :
1、	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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
    <context:annotation-config/>
    <!--            -->
   <context:component-scan base-package="com.spring"></context:component-scan>
</beans>

2、	Java  :
       @Component。             name   userService
           getBean(“userService”)  。       userService       bean key userServiceImpl           UserServiceImpl     
package com.spring.service;
import org.springframework.stereotype.Component;
import com.spring.dao.UserDao;

@Component("userService")
public class UserServiceImpl implements UserService{
	private UserDao userDao;
	public void init(){
		System.out.println("init");
	}
	public void destory(){
		System.out.println("destory");
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public void HelloWorld(){
		System.out.println("helloworld");
	}
}

  @Resource       
                。          @Resource             。    :
@Component("userAction")
public class UserAction {
	private UserService userService;
	
	//   beans xml        
	//     name    byName   
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public void addUser(){
		userService.HelloWorld();
	}
}


    :
       userAction                   ,       @ Component   UserAction           。                getbean  

	@Test
	public void test01(){
		BeanFactory applicationContext = new ClassPathXmlApplicationContext("beans.xml");
		UserAction user = (UserAction) applicationContext.getBean("userAction");
		user.addUser();
	}


@Scope @PostConstruct、@PreDestroy

                initmethod destorymethod      。    :
@Scope("Singleton")
@Component("userService")
public class UserServiceImpl implements UserService{
	private UserDao userDao;
	@PostConstruct
	public void init(){
		System.out.println("init");
	}
	@PreDestroy
	public void destory(){
		System.out.println("destory");
	}
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public void HelloWorld(){
		System.out.println("helloworld");
	}
}

좋은 웹페이지 즐겨찾기