[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" />  

 

转载于:https://www.cnblogs.com/vickylinj/p/9519353.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值