spring.xml及注解

spring.xml配置文件中配置注解:

开启注解(及自动扫描包中bean):

1:<context:component-scan base-package="com.bzu" />    在base-packge指定所需要扫描的包,建议指定一个包含整个架构的包,可以扫描到各层所定义的bean;

或2:<context:annotation-config />   2种方法选一;

引入外部properties文件,常为数据库连接配置文件;

1:<bean id="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>

或2 :<context:property-placeholder location="classpath:redis.properties" />

@Component:只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean(可以在xml中去掉bean定义的配置):

使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是userDaoImpl。你也可以指定Bean的名称:

@Component("userDao")
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与@Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、@Service、@Controller来替代@Component。

通过@Autowired或@Resource来实现在Bean中自动注入的功能

在java代码中使用@Autowired或@Resource注解方式进行装配 ,这两个注解的区别是:@Autowired默认按类型装配,@Resource默认按名称装配,当找不到名称匹配的bean才会按类型装配。
@Autowired一般装配在set方法之上,也可以装配在属性上边,但是在属性上边配置,破坏了java的封装,所以一般不建议使用

1 public class AbstractBaseRedisDao<K, V> {
2
3     @Autowired
4     protected RedisTemplate<K, V> redisTemplate;
5
6     public void setRedisTemplate(RedisTemplate<K, V> redisTemplate) {
7         this.redisTemplate = redisTemplate;
8     }

@Resource

的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略
@Resource装配顺序

1 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

2 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

3 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

4 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);如果没有匹配,则回退为一个原始类型(UserDao)进行匹配,如果匹配则自动装配;

 1 @Service("deptService")
 2 @Transactional
 3 public class DeptServiceImpl implements IDeptService {
 4     @Resource
 5     private IDeptDao deptDao;
 6
 7     public IDeptDao getDeptDao() {
 8         return deptDao;
 9     }
10
11     public void setDeptDao(IDeptDao deptDao) {
12         this.deptDao = deptDao;
13     }

基于hibernate的事物:

<!-- 配置SessionFactory(整合Hibernate) -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>  //dataSource数据连接配置
</bean>
<!-- 配置基于注解的事务支持-->
<tx:annotation-driven transaction-manager="transactionManager"/>

基于spring mvc的事物:

<!-- spring和MyBatis整合,不需要mybatis的配置映射文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--自动扫描mapping.xml文件-->
<property name="mapperLocations" value="classpath:cn/jbit/mybatisdemo/dao/*.xml"></property>
</bean>

<!--事物管理-->
<bean name="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />     
</bean>

<!--注解方式配置事物-->
<tx:annotation-driven transaction-manager="transactionManager" />

给service层的实现类加上@Transactional,实现事物管理;

时间: 2024-07-29 15:14:49

spring.xml及注解的相关文章

Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)

陈科肇--http://blog.csdn.net/u013474104/article/details/44352765 ======= 1.装配术语 创建应用对象之间协作关系的行为通常被称为装配 2.使用注解装配 Spring是从Spring2.5开始引入使用注解自动装配的. Spring容器是默认禁用注解装配的,因此如果要使用Spring的注解装配,你必须启用它.启用方式:使用Spring的context命名空间配置中的<context:annotation-config>元素,配置启用

JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试

Spring框架学习路线 Spring的IOC Spring的AOP,AspectJ Spring的事务管理,三大框架的整合 Spring框架概述 什么是Spring? Spring是分层的JavaSE/EE full-stack(一站式)轻量级开源框架. 所谓分层: SUN提供的EE的三层结构:web层.业务层.数据访问层(也称持久层,集成层). Struts2是web层基于MVC设计模式框架. Hibernate是持久的一个ORM的框架. 所谓一站式:Spring框架有对三层的每层解决方案.

Spring集成Hibernate(基于XML和注解配置)

配置Hibernate <?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.springfram

Spring MVC常用注解

cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示.在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Control

spring AOP + 自定义注解实现权限控制小例子

今天看了一下黑马程序员的视频,上面讲到一个使用spring AOP + 自定义注解的方式来实现权限控制的一个小例子,个人觉得还是可以借鉴,整理出来与大家分享. 需求:service层有一些方法,这些方法需要不同的权限才能访问. 实现方案:自定义一个PrivilegeInfo的注解,使用这个注解为service层中的方法进行权限配置,在aop中根据PrivilegeInfo注解的值,判断用户是否拥有访问目标方法的权限,有则访问目标方法,没有则给出提示. 关键技术:自定义注解及注解解析,spring

Java Spring的 JavaConfig 注解

序 传统spring一般都是基于xml配置的,不过后来新增了许多JavaConfig的注解.特别是springboot,基本都是清一色的java config,不了解一下,还真是不适应.这里备注一下. @RestController spring4为了更方便的支持restfull应用的开发,新增了RestController的注解,比Controller注解多的功能就是给底下的RequestMapping方法默认都加上ResponseBody注解,省得自己再去每个去添加该注解. @Configu

Spring 的@Scheduled注解实现定时任务运行和调度

首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xml或context.xml) xmlns 多加以下的内容. [html] view plaincopy xmlns:task="http://www.springframework.org/schema/task" 然后xsi:schemaLocation多加以下的内容. [html] view plaincopy http://www.springf

6.Spring+Struts+Hibernat注解方式整合

SSH注解方式实现 1.创建db.properties文件,主要是数据库的连接数据 jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc\:mysql\://localhost\:3306/testjdbc.username=rootjdbc.password=076634 2.创建Struts的配置文件,主要定义struts的常规配置 <?xml version="1.0" encoding="UTF-8&qu

Spring AOP基于注解的“零配置”方式

Spring AOP基于注解的“零配置”方式: Spring的beans.xml中 <!-- 指定自动搜索Bean组件.自动搜索切面类 --> <context:component-scan base-package="org.crazyit.app.service,org.crazyit.app.aspect"> <context:include-filter type="annotation" expression="or