Spring的注解

  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="300"></property>
        <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
        <property name="maxIdleTime" value="900"></property>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="2"></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">

  注入成功。

原文地址:https://www.cnblogs.com/yncx/p/5967504.html