Action想使用serviceImpl时,都需要最原始的方法New一个接口,Service service = new serviceImpl();去实例化service了。都需要Action主动创建对象,将userService实例化。

  而加入了Spring后,当Action需要ServiceImpl注入时,需要通过Spring将ServiceImpl实现注入。

  由原先的自动创建升级为自动注入。这就是Spring的核心思想控制反转,也称为依赖注入。

  原生版applicationContext.xml。

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"> <!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <bean id="mySessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池,包含了数据库用户名,密码等等信息 -->
<property name="dataSource" ref="myDataSource" /> <!-- 配置Hibernate的其他的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 开机自动生成表 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>news/entity/News.hbm.xml</value>
</list>
</property> </bean> <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<!-- 每300秒检查所有连接池中的空闲连接 -->
<property name="idleConnectionTestPeriod" value=""></property>
<!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
<property name="maxIdleTime" value=""></property>
<!-- 最大连接数 -->
<property name="maxPoolSize" value=""></property> </bean> <bean id="myNewsAction" class="news.action.NewsAction" scope="prototype">
<property name="ns" ref="myNewsService"></property>
</bean> <bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype">
<property name="nd" ref="myNewsDao"></property>
</bean> <bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory" />
</bean>
</beans>

  删除以下代码,使用注解代替。

<bean id="myNewsAction" class="news.action.NewsAction" scope="prototype">
<property name="ns" ref="myNewsService"></property>
</bean> <bean id="myNewsService" class="news.service.NewsServiceImpl" scope="prototype">
<property name="nd" ref="myNewsDao"></property>
</bean> <bean id="myNewsDao" class="news.dao.NewsDaoImpl" scope="prototype">
<property name="sf" ref="mySessionFactory" />
</bean>

  1.通过在类上使用 @Scope、@Controller、@Repository 注解,Spring 将自动创建相应的BeanDefinition对象,并在ApplicationContext中声明。从而便于注入。

  @Scope("prototype")即非单例,每次请求都创建一个独立的Action,避免strutsAction的线程安全问题。

  @Controller用于对控制层标注,也就是Action。

@Controller               //默认就是类的首字母小写newsAction
@Scope("prototype")
public class NewsAction extends ActionSupport {
……
}

  @Service用于对业务层标注,也就是service。

@Repository
@Scope("prototype")
public class NewsDaoImpl implements NewsDao {
……
}

  @Repository用于对数据持久层标注,也就是Dao。

@Service
@Scope("prototype")
public class NewsServiceImpl implements NewsService {
……
}

  2.需要加入一个自动扫描包(也会自动注入解释器),所以不需要 context:annotation-config(自动注入processor解析器)。

  <context:component-scan base-package="news"></context:component-scan>

  3.原来的set的方法:(永远不要忘记没使用注解时,需要set方法进行注入)。(也不要忘记最原始的NewsService ns = new NewsServiceImpl();)

private NewsService ns;
public void setNs(NewsService ns) {
this.ns = ns;
}

  改为@Autowired实现注入。

@Autowired
private NewsService ns;
@Autowired
private NewsDao nd;
@Autowired
private SessionFactory sf;

  还需要修改一下applicationContext.xml。修改一下sessionFactory的id,即

<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">

  注入成功。

Spring的注解的更多相关文章

  1. Spring MVC注解的一些案列

    1.  spring MVC-annotation(注解)的配置文件ApplicationContext.xml <?xml version="1.0" encoding=& ...

  2. Spring系列之Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...

  3. spring @condition 注解

    spring @condition注解是用来在不同条件下注入不同实现的 demo如下: package com.foreveross.service.weixin.test.condition; im ...

  4. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  5. Spring的注解方式实现AOP

    Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...

  6. Spring 之注解事务 @Transactional

    众所周知的ACID属性:  原子性(atomicity).一致性(consistency).隔离性(isolation)以及持久性(durability).我们无法控制一致性.原子性以及持久性,但可以 ...

  7. 数据库事务中的隔离级别和锁+spring Transactional注解

    数据库事务中的隔离级别和锁 数据库事务在后端开发中占非常重要的地位,如何确保数据读取的正确性.安全性也是我们需要研究的问题.ACID首先总结一下数据库事务正确执行的四个要素(ACID): 原子性(At ...

  8. Spring JSR-250注解

    Java EE5中引入了“Java平台的公共注解(Common Annotations for the Java Platform)”,而且该公共注解从Java SE 6一开始就被包含其中. 2006 ...

  9. 【SSM 2】spring常用注解

    声明:以下观点,纯依据个人目前的经验和理解,有不当之处,多指教! 一.基本概述 注解(Annotation):也叫元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举 ...

  10. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

随机推荐

  1. django静态文件配置

    开发环境配置 需要下面几个步骤 1. 在app目录下创建static目录,将静态文件和相关文件夹放到此目录下,如your_app/static/img等 2. 确保settings.py中的INSTA ...

  2. JAG Summer 2012 Day 4 C Connect

    状压dp,由于枚举两维状态会GG,所以只枚举当前位置前m个的状态,就是这个样子大概= =: 呆马: #include <iostream> #include <cstdio> ...

  3. C语言 05 数组

    数组作为函数参数,可以省略元素个数. 数组作为函数参数,传递是整个数组的地址,修改函数形参数组的值,会影响到外面的 实参数组. 基本数据类型作为函数参数,传递是数值.

  4. 鸟哥的linux私房菜学习记录之软件安装原始码与Tarball

  5. 巧用margin/padding的百分比值实现高度自适应(多用于占位,避免闪烁)

    本文依赖于一个基础却又容易混淆的css知识点:当margin/padding取形式为百分比的值时,无论是left/right,还是top/bottom,都是以父元素的width为参照物的!也许你会说, ...

  6. uboot补丁的分析

    接下来分析一下韦老师的uboot补丁: -------------------------------------------------------------------------------- ...

  7. js爬虫

    1.爬虫相关的包 (1)const request =  require('superagent'); // 处理get post put delete head 请求  轻量接http请求库,模仿浏 ...

  8. 简述id,instancetype和__kindof的区别

    id: 好处:可以调用任何对象方法 坏处:不能进行编译检查 + (id)person; instancetype 好处:自动识别当前类的对象 坏处:不会提示返回的类型 + (instancetype) ...

  9. h5网页中使用打电话功能

    如果需要在移动浏览器中实现拨打电话,发送email,美国服务器,调用sns等功能,移动手机WEB页面(HTML5)Javascript提供的接口是一个好办法. 采用url链接的方式,实现在Safari ...

  10. p12(PKCS12)和jks互相转换

    p12 -> jks keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -deststoretype ...