简体   繁体   English

在没有接线的情况下使用春豆的其他选择是什么

[英]What is other alternative to use spring bean without wiring

I am using this in my UserDAO and hibernate 我在UserDAO中使用它并休眠

@Resource(name="sessionFactory");
public SessionFactory sessionFactory;

For some reason i am not able to link spring beans in my userDAO from 2 different classes. 由于某些原因,我无法从2个不同的类在userDAO中链接Spring bean。

What is the other alternative of above code i mean something like 以上代码的另一种选择是什么,我的意思是

SessionFactory sessionFactory = new Sessionfactory()

is that ok 这可以吗

web.xml web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>testing</display-name>


  <filter>
            <filter-name>springSecurityFilterChain</filter-name>
            <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
            <filter-name>springSecurityFilterChain</filter-name>
            <url-pattern>/*</url-pattern>
    </filter-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/spring-security.xml

        </param-value>
    </context-param>




  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/krams/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

dispatcher-servlet 调度程序

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.0.xsd">


    <import resource="hibernate-context.xml" />


    <mvc:annotation-driven /> 

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages" />

    <context:component-scan base-package="com.vaannila" />

    <bean id="userService" class="com.vaannila.service.UserServiceImpl" />

    <bean id="userValidator" class="com.vaannila.validator.UserValidator" />
     <bean id="registrationDAO" class="com.vaannila.dao.RegistrationDAO" />
     <bean id="userDAO" class="com.vaannila.dao.UserDAO" />





   <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
          resource.loader=class
          class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
         </value>
      </property>
   </bean>



</beans>

hibernate-context 休眠上下文

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

    <context:property-placeholder location="/WEB-INF/spring.properties" />

    <!-- Enable annotation style of managing transactions -->
    <tx:annotation-driven transaction-manager="transactionManager" />   

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
    <!-- See http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/orm/hibernate3/annotation/AnnotationSessionFactoryBean.html -->                           
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/SessionFactory.html -->
    <!-- See http://docs.jboss.org/hibernate/stable/core/api/index.html?org/hibernate/Session.html -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.vaannila"/>

    <!-- Declare a datasource that has pooling capabilities-->   
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
                destroy-method="close"
                p:driverClass="${app.jdbc.driverClassName}"
                p:jdbcUrl="${app.jdbc.url}"
                p:user="${app.jdbc.username}"
                p:password="${app.jdbc.password}"
                p:acquireIncrement="5"
                p:idleConnectionTestPeriod="60"
                p:maxPoolSize="100"
                p:maxStatements="50"
                p:minPoolSize="10" />

    <!-- Declare a transaction manager-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
                p:sessionFactory-ref="sessionFactory" />

</beans>

No, that is not OK. 不,那不行。 The fresh SessionFactory won't be initialized properly (it lacks all the required configurations that you have set in applicationContext.xml ). 新的SessionFactory无法正确初始化(它缺少您在applicationContext.xml设置的所有必需配置)。

So make sure you have a bean named sessionFactory and <context:component-scan .. /> 因此,请确保您有一个名为sessionFactory<context:component-scan .. />的bean <context:component-scan .. />

Also note that beans defined in dispatcher-servlet.xml are not visible for beans defined in applicationContext.xml 还要注意,对于applicationContext.xml定义的bean,看不到dispatcher-servlet.xml中定义的bean。

No that is not ok. 不,那不行。 You can use autowire feature of spring. 您可以使用弹簧的自动接线功能。

In case you don't like to use annotation for linking, consider using application-context.xml for linking(injecting) your sessionFactory. 如果您不想使用注释进行链接,请考虑使用application-context.xml来链接(注入)您的sessionFactory。

You are not providing enough context. 您没有提供足够的上下文。 I don't even understand what your problem is, nor what's not working. 我什至不了解您的问题是什么,也不起作用。 Please show some configuration and some output. 请显示一些配置和一些输出。

But: 但:

SessionFactory sessionFactory = new Sessionfactory()

If you are using code like this, there's no point of using an IOC container like Spring in the first place, because you are missing out on the key concept, dependency injection. 如果您使用的是这样的代码,则首先没有必要使用像Spring这样的IOC容器,因为您错过了关键概念依赖注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM