[spring]Bean注入——使用注解代替xml配置


使用注解編程,主要是為了替代xml文件,使開發更加快速。

 

一、使用注解前提:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
//1:使用注解,需要在配置文件中增加命名空間和約束文件(Spring提供了一個context的命名空間,它提供了通過掃描類包以應用注解定義Bean的方式) xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> //2:告訴框架那些類是使用注解的。 <!-- 激活組件掃描功能,掃描aop的相關組件組件 --> <context:component-scan base-package="com.test.core.aop" /> ... </beans>

二、注解與xml配置的對應關系

注解

Package com.lsz.spring; @Repository public class UserDao{ //。。。。 
} 

對應的xml配置文件

<bean id="userDao" class="com.lsz.spring.UserDao" />  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM