applicationContext.xml和spring-servlet.xml在Spring框架无论如何有关系吗?applicationContext.xml是否可用DispatcherServlet?*-servlet.xml?为什么applicationContext.xml单独不足?应用程序上下文和Web应用程序上下文有什么区别?
我知道WebApplicationContext用于面向Spring MVC架构的应用程序?
我想知道ApplicationContextMVC应用程序有什么用?什么样的豆类定义ApplicationContext?
我有一个带有applicationContext.xml和dispatcher-servlet.xml配置的Spring Web应用程序.我<context:component-scan />在applicationContext.xml中定义了,但是当我运行我的应用程序时,除非我也添加<context:component-scan />到dispatcher-servlet.xml,否则找不到控制器.我在两者中使用相同的基础包,所以这不是问题.
我很困惑,因为我认为 applicationContext.xml是dispatcher-servlet.xml的父级.不会把<context:component-scan />applicationContext.xml放进去吗?
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
编辑:我也在dispatcher-servlet.xml中使用mvc:annotation-driven,它应该选择控制器(我想?).
编辑2:这是配置文件.我从applicationContext.xml中删除了一堆Spring Security和OAuth设置(出于安全原因,他们可能无论如何都不相关).
applicationContext.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context" xmlns:oauth="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="bar.foo"/>
<context:property-placeholder location="classpath:my.properties" />
<bean class="bar.foo.ServicesConfig" />
</beans>
Run Code Online (Sandbox Code Playgroud)
调度员servlet.xml中
<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:c="http://www.springframework.org/schema/c" …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring 3和Hibernate 3.我正在尝试配置Spring声明式事务,但无论我尝试什么,Spring事务都没有开始.
这是我的配置
文件:applicationContext-hibernate.xml
<tx:annotation-driven transaction-manager="txManager" />
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="mdbDataSource" class="org.apache.commons.dbcp.BasicDataSource">
...
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mdbDataSource" />
<property name="annotatedClasses">
.....
</bean>
Run Code Online (Sandbox Code Playgroud)
我有一个ServiceLocatorImpl类,它实现了ServiceLocator接口
@Service("serviceLocator")
@Transactional
public class ServiceLocatorImpl implements ApplicationContextAware, Serializable, ServletContextAware, ServiceLocator {
public ResultObject executeService( Map objArgs )
{
if(TransactionSynchronizationManager.isActualTransactionActive()) {
LOGGER.debug("ServiceLocator:executeService - Active transaction found");
} else {
LOGGER.error("No active transaction found");
}
......
}
....
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我的所有配置都是正确的.但是,当调用executeService方法时,TransactionSynchronizationManager.isActualTransactionActive()始终返回false.
请帮我解决这个问题.如果需要更多信息,请告诉我.
更新: 我已将ServiceLocator连接到其他类之一,如下所示:
@Autowired
private ServiceLocator serviceLocator; // ServiceLocator …Run Code Online (Sandbox Code Playgroud) 我在JPA实体中有延迟加载属性的问题.我读了许多类似的问题,但它们与spring或hibernate有关,它们的问题不适用或有用.
该应用程序是JEE,在Wildfly应用程序服务器上运行JPA2.1.有两个实体,DAO会话bean和servlet将它们组合在一起:
@Entity
@Table(name = "base_user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
int id;
@OneToMany(fetch=FetchType.LAZY, mappedBy="user")
List<OAuthLogin> oauthLogins;
}
@Entity
@Table(name = "oauth_login")
public class OAuthLogin implements Serializable {
@ManyToOne
@JoinColumn(name="user_id", nullable=false)
User user;
}
@Stateless(name = "UserDAOEJB")
public class UserDAO {
@PersistenceContext(unitName="OAUTHDEMO")
EntityManager em;
public User findById(int id) {
User entity;
entity = em.find(User.class, id);
return entity;
}
}
public class SaveUserServlet extends HttpServlet {
@EJB
UserDAO userDAO;
@Transactional
protected void doPost(HttpServletRequest …Run Code Online (Sandbox Code Playgroud)