大鸵鸟 2016-04-15 04:19 采纳率: 0%
浏览 1379
已结题

spring会自动代理Struts2的action

我的struts的action全部被spring配置成了aop,我的action出现的全部是代理对象。
导致我的action不能调用自己定义的方法。为什么我没有配置struts的action为aop,
但是spring全部自动配置成了aop?这spring的配置文件

 <?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:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 启用注解扫描 -->
<context:annotation-config/>
<!-- spring默认只代理Action类的接口的函数,cglib jar文件就是可以代理Action自己定义的函数 
<aop:aspectj-autoproxy proxy-target-class="true" />
-->
<bean id="testService" class="com.guoqiao.test.TestService">
    <property name="name" value="test"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orclguo"/>
        <property name="username" value="scott"/>
        <property name="password" value="tiger"/>
         <!-- 连接池启动时的初始值 -->
        <property name="initialSize" value="30"/>
        <!-- 连接池的最大值 -->
        <property name="maxActive" value="500"/> 
        <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
        <property name="maxIdle" value="2"/>
        <!--  最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
        <property name="minIdle" value="1"/>
</bean>
<!-- 配置会话工厂() -->
<bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     <!-- 设置数据源 -->
     <property name="dataSource" ref="dataSource"/>
     <!-- 接管了hibernate对象映射文件 -->
     <property name="mappingResources">
        <list>     
          <value>com/guoqiao/domain/Department.hbm.xml</value>
          <value>com/guoqiao/domain/Employee.hbm.xml</value>
        </list>
     </property>
     <property name="hibernateProperties">
        <value>
                hibernate.dialect=org.hibernate.dialect.OracleDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
                hibernate.cache.use_second_level_cache=true
                hibernate.cache.use_query_cache=true
                hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
                hibernate.generate_statistics=true        
        </value>
     </property>
</bean>
<!--
<bean id="employeeService" class="com.guoqiao.service.imp.EmployeeService">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
  -->
<!-- 配置EmployeeService对象通过注解的方式来注入属性值,这是一个知识点 -->
<bean id="employeeService" class="com.guoqiao.service.imp.EmployeeService"/>

<bean id="departmentService" class="com.guoqiao.service.imp.DepartmentService">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器,统一管理sessionFactory的事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 启用事务注解 @Transactional-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>

明白点说就是我spring中的bean全部都被spring代理了,包括我struts2的action.
举个例子,我有个叫employee的bean

 <bean id="employeeService" class="com.guoqiao.service.imp.EmployeeService">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

public interface EmployeeServiceInterf{
public void a();
}
public class EmpolyeeService implements EmployeeServiceInterf{
public void b(){
System.out.println("b");
};
public void a(){
System.out.println("a");
};
}

public class TestInjection(){
//下面的写法注入不了
EmpoyeeService employeeService;
//下面的写法可以注入
EmpoyeeServiceInterf employeeService;
}
这是一个bean,这个Bean类是EmpoyeeService
EmpoyeeService实现了EmployeeServiceInterf接口
但我是把这个bean注入类型为EmpoyeeService的对象上会注入失败
把这个bean注入EmployeeServiceInterf的对象上时才能成功(在spring集成struts2的环境中)

再明白点说就是说这个bean看起来像是一个实现类,但其实你用这bean注入其他对象
上的时候,这个bean是这个实现类的代理对象。

如果上面我手动写的代码还看不懂的话,你还是绕道吧,你连我讲的是什么都看不懂。

  • 写回答

3条回答

  • haixuan1137 2016-04-15 05:12
    关注

    /context:component-scan这个才是扫描注释的

    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机