- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 UserDAO 和 hibernate 中使用它
@Resource(name="sessionFactory");
public SessionFactory sessionFactory;
出于某种原因,我无法在我的 userDAO 中从 2 个不同的类链接 spring bean。
上述代码的其他替代方案是什么,我的意思是这样的
SessionFactory sessionFactory = new Sessionfactory()
可以吗
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>
调度程序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 上下文
<?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>
最佳答案
不,那不行。新鲜SessionFactory
将无法正确初始化(它缺少您在 applicationContext.xml
中设置的所有必需配置)。
所以请确保您有一个名为 sessionFactory
的 bean和<context:component-scan .. />
另请注意 dispatcher-servlet.xml
中定义的 beans对于applicationContext.xml
中定义的bean不可见
关于java - 使用 spring bean 无需接线的其他替代方案是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5416544/
使用 Spring 连接,如果我有一个接口(interface)的多个实现,我可以使用 @Qualifier 来指定我想要的。 例如,假设我有一个 @Component @Qualifier("Toy
我正在尝试将构造函数注入(inject)到下面的 bean 中,但我一直收到未找到默认构造函数的错误 - Caused by: java.lang.NoSuchMethodException: my.
我正在学习 Spring 的基础知识,现在我正在复习 Beans/wiring。这个问题可能没有意义,这只是我在阅读/尝试 Craig Walls 的 Spring into Action 一书中的一
1、软路由、硬路由概念介绍 硬路由 通俗介绍:目前我们上网使用的路由器就是“硬”路由,他是厂商提供了一套完整的定制的软件+硬件的解决方案,包含刷好的系统(固件)、电源、路由器机
我试图找出使用 akka-http 和 akka-streams 实现真正的 websocket 应用程序的最佳方法。我主要寻找的是简单性,而我现在还没有得到。 假设您有一个相当复杂的管道,它需要区分
只是在阅读spring,并且在spring中使用DI时,如果将bean设置为单例(默认),则将“分配”该类的单个实例,而原型(prototype)每次都会强制一个新实例。 每次容器分配相同实例的后果是
我的项目有两个 ApplicationContext(非常大的项目)。一份带有数据的旧 xml 现在我需要将我的其他项目 applicatinContext 添加到其中或以任何其他方式添加,这
有没有一种方法或工具可以在编译时检测我的任何 spring bean 是否未正确 Autowiring 。 例如,对于基于类型的布线,如果该类型不存在,它应该提示。或者对于基于“名称”的连接,如果不存
我现在在我的网站上使用 Typescript,但在处理特定的 jQuery 事件时遇到困难。 为了举例,我将我的代码缩短为只包含相关部分;我有这样的设置... var elements = {
是否可以通过JUnit测试Spring接线是否成功? 我想通过反射(reflection)来做到这一点。例如:获取所有带有 id *Controller 的 bean 并测试字段 *services
为什么我的集成测试提示缺少 RestTemplate接线Spring Boot ? 我能做些什么来修复这个集成测试? 此外,这是集成测试还是单元测试?根据我的理解,这个测试正在调用一个模拟服务器,它是
我正在关注本教程:http://www.youtube.com/watch?v=i9MHigUZKEM 我已经 46:32 分钟了,这是到目前为止我的代码: Name :
我正在使用 Node 和 Anugular,并且我已经从我的应用程序创建了一个 RESTful api,并创建了一个 Angular 资源来使用它。我对 Angular ui-router 指令如何与
我尝试使用 GPIO 引脚 18 上的 PWM 来驱动 Raspberry PI 的伺服系统,我对设置进行了接线,如下所示。 当我驱动伺服时,我可以毫无问题地执行此操作,我使用的命令如下所示。
我是一名优秀的程序员,十分优秀!