Spring 基於註解的開發

Spring基於註解的開發



  註解就是一個類,使用@註解名稱,用於取代xml配置文件。

  (一)普通註解:@Component("id") 取代 <beanid="" class="">  【見例一】

  (二)web開發提供三個註解: 【見例二】

     (1)@Repository :dao層

     (2)@Service:service層

     (3)@Controller:web層

(三)依賴注入(給私有字段或者是setter方法設值)

      (1)普通值 @Value("")

      (2)引用值:

           按照類型@Autowired,

           按照名稱@Autowired

                    @Qualifier("名稱")

           按照名稱 @Resource("名稱")

   (四)生命週期  【見例三】

             初始化:@PostConstruct

             銷燬:@PreDestroy                    

   (五)作用域:@Scope("prototype")多例

 

【注意】使用註解的前提,添加命名空間,讓Spring掃描含有註解的類。

<beansxmlns="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:component-scan base-package="?"></context:component-scan>

 

例一:使用@Component("id") 取代<bean id="" class="">

      步驟:編寫User接口,編別UserImlp實現類,編寫xml,編寫測試類。

 

public interface User {
	public void add();	
}

@Component("UserImpleId")
public class UserImple implements User {
	@Override
	public void add() {
		// TODO Auto-generated method stub
		System.out.println("annotion addUser");
	}

}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_a_component"></context:component-scan>
</beans>

public class Test {

	@org.junit.Test
	public void test() {
		String xmlPath = "com/Lily/SpringLearning/f_annotation_a_component/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		User u = (User) applicationContext.getBean("UserImpleId");
		u.add();
	}

}


例二:web開發的三個註解

步驟:

1、編寫my_service接口

2、編寫 my_dao接口

3、編寫my_daoImpl實現類,並註解@Repository("mydaoId")

4、編程my_serviceImpl實現類,並提供註解@Service,在調用dao的方法前面加上註解:@Autowired      @Qualifier("mydaoId ") 【按照名稱注入】

5、編寫action類,並提供註解@Controller("ActionId") 在調用service服務前 註解@Autowired

6、編寫xml,掃描包。

7、編寫測試類,這裏getBean需要get的是action的Id

 

public interface my_Service {
	void add();
}

public interface my_dao {
	void save();
}

@Repository("mydaoid")
public class my_daoImpl implements my_dao {

	/* (non-Javadoc)
	 * @see com.Lily.SpringLearning.f_annotation_b_web.my_dao#save()
	 */
	@Override
	public void save() {
		// TODO Auto-generated method stub
		System.out.println("Dao save() work! ");
	}

}

@Service
public class my_serviceImpl implements my_Service {

	private my_dao dao;
	

	@Autowired
	@Qualifier("mydaoid")
	public void setDao(my_dao dao) {
		this.dao = dao;
	}



	@Override
	public void add() {
		dao.save();

	}

}

@Controller("myactionId")
public class my_Aciton {
	
	@Autowired
	private my_Service s;
	public void DoAction(){
		s.add();
		
	}
}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_b_web"></context:component-scan>

</beans>

public class Test {

	@org.junit.Test
	public void test() {
		String xmlpath="com/Lily/SpringLearning/f_annotation_b_web/beans.xml";
		ApplicationContext ap=new ClassPathXmlApplicationContext(xmlpath);
		my_Aciton ac=(my_Aciton) ap.getBean("myactionId");
		ac.DoAction();		
	}}


例三 生命週期:初始化和銷燬

步驟:

1、寫一個User接口

2、寫UserImple實現類,並在類名前加上註解@Service("userId") ,然後在初始化類前面加上註解@PostConstruct,在銷燬方法前加上註解 @PreDestroy

3、編寫xml掃描組件

4、編寫測試類,【注意】這裏的測試類的getBean方法和以前的不太一樣,要用類.class來獲得!

 

public interface User {
	public void add();
}

@Service("UserId")
public class UserImpl implements User {

	/* (non-Javadoc)
	 * @see com.Lily.SpringLearning.f_annotation_c_lifecycle.User#add()
	 */
	@Override
	public void add() {
		System.out.println("LifeCycle   User  add() ");
	}
	
	@PostConstruct
	public void myInit(){
		System.out.println("init() !");
	}
	
	@PreDestroy
	public void myDestory(){
		System.out.println("destory() !");
	}
	

}

<?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:component-scan base-package="com.Lily.SpringLearning.f_annotation_c_lifecycle"></context:component-scan>

</beans>

public class Test {

	@org.junit.Test
	public void test() {
		String xmlPath = "com/Lily/SpringLearning/f_annotation_c_lifecycle/beans.xml";
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		User user = applicationContext.getBean("UserId" ,User.class);
		User user2 = applicationContext.getBean("UserId" ,User.class);
		
		System.out.println(user);
		System.out.println(user2);
		
		applicationContext.close();
	}

}


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