如何開啟spring框架以注解形式的配置


步驟

  • 導包(新版本需要導入spring-aop-4.3.17.RELEASE.jar)
  • 為配置文件applicationContext.xml引入新的命名空間(約束)
  • 開啟使用注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ">

	<!-- 指定掃描com.fei.bean包下的所有類中的注解  -->
	<context:component-scan base-package="com.fei.bean"></context:component-scan>
	
</beans>

注意:會掃描指定包下的所有子孫包

  • 在類中使用注解完成配置
具體注解類型(將對象注冊到容器),寫在類前面的
@Component("user")
// 上面的注解配置相當於xml文件中的<bean name="user" class="com.fei.bean.User"></bean>
	@Service("user")	// service層
	@Controller("user")	// web層
	@Repository("user")	// dao層
上面三種方式的效果都是一樣的,都是將對象注冊到容器,只是用不同的名字用於區分對象屬於不同的層


免責聲明!

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



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