我正在学习春天3,我似乎没有把握背后的功能<context:annotation-config>和<context:component-scan>.
根据我的阅读,他们似乎处理不同的注释(@ Required,@ Autowired etc vs @Component,@ Repository,@ Service等),但也从我读过的内容中注册了相同的bean后处理器类.
为了让我更加困惑,有一个@Required属性@Autowired.
有人可以对这些标签有所了解吗?什么是相似的,什么是不同的,一个被另一个取代,它们相互完成,我需要其中一个,两者都有吗?
我已经开始学习Spring MVC阅读本教程:http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/
好的,这对我来说非常清楚.
在这个例子中,我有web.xml文件来配置我的Web应用程序:
<?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>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
以及用于配置mu DispatcherServlet 的spring-servlet.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: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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="net.viralpatel.spring3.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Run Code Online (Sandbox Code Playgroud)
而且,正如你在previus链接中看到的那样,我只有一个控制器类来处理"/ hello"URL的HTTP请求......好吧......这对我来说很明显......
在这个例子旁边,我通过STS\Eclipse中的相关模板项目创建了一个新的Spring MVC项目.
此示例与te previus非常相似:我总是使用web.xml文件来配置我的Web应用程序,配置DispatcherServlet的文件和处理HTTP请求的控制器类.
但是我有些不同,我无法理解.
这是我的 …
我正在尝试使用数据库表在我的spring应用程序中应用安全性.
到目前为止,我在applicationContext-Security中所拥有的是:
<beans:bean id="userDetailsService" class="org.intan.pedigree.service.UserDetailsServiceImpl"></beans:bean>
<http auto-config='true'>
<intercept-url pattern="/**" access="ROLE_USER" />
</http>
<beans:bean id="daoAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
<beans:property name="userDetailsService" ref="userDetailsService" />
</beans:bean>
<beans:bean id="authenticationManager"
class="org.springframework.security.authentication.ProviderManager">
<beans:property name="providers">
<beans:list>
<beans:ref local="daoAuthenticationProvider" />
</beans:list>
</beans:property>
</beans:bean>
<authentication-manager>
<authentication-provider user-service-ref="userDetailsService">
<password-encoder hash="plaintext" />
</authentication-provider>
</authentication-manager>
Run Code Online (Sandbox Code Playgroud)
我的userDetailsService实现如下:
package org.intan.pedigree.service;
import org.intan.pedigree.dao.UserEntityDAO;
import org.intan.pedigree.dao.UserEntityDAOImpl;
import org.intan.pedigree.form.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserEntityDAO dao;
@Autowired
private Assembler assembler;
@Transactional(readOnly = …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个更好地理解这个问题.解决方法非常简单,即将配置数据移动到另一个没有包含代理/建议的类,但我认为更好地理解它将帮助我避免将来出现其他相关问题,所以我想要任何解释任何人可以提供.
我正在使用Spring 3.1.0.RELEASE与Spring STS和vFabric tc服务器.使用@Controller类实现了一个基本的小型REST服务器.这是所有伟大的(实际上,它是),但@Controller也@Transactional,并且和负载时编织和TC的vFabric服务器之间,它打破@Value.
@Controller
@RequestMapping("/hello")
public class MyAPI {
@Value("${my.property}")
private String prop;
...
@Transactional
handleRequest(...) ...
}
Run Code Online (Sandbox Code Playgroud)
和属性文件app.properties:
my.property = SUCCESS
Run Code Online (Sandbox Code Playgroud)
这在JUnit下工作正常,测试得到一个支持设置为"SUCCESS"的MyAPI对象.但是当应用程序加载到vFabric中时,我猜它会加载时间编织和代理.无论发生什么事情,有创造了两个MyAPI情况下,一个拥有道具=="成功"和另一个(这是很不幸的是处理HTTP请求中的一个),其中有托=="$ {} my.prop".
总而言之,我称之为魔术的失败,这是我使用像AOP这样的东西最大的担忧.即使使用STS,我也不知道如何找出问题背后的原因,或者弄清楚这是否是一个严重的错误.如果它是一个bug,我不知道它是Spring,AspectJ,加载时织入器还是vFabric中的bug,所以我甚至不知道在哪里提交bug报告.
因此,理解这一点的任何帮助将不胜感激.谢谢.
我有一个问题,Spring将DAO对象的代理注入到服务中,但是这个服务被注入到控制器中它是具体的类.这不允许我使用服务范围的事务并分别为每个DAO调用启动事务.这是我期望的行为.
组态:
Controller是带有@Controller注释和构造函数DI的类.
服务:
@Component
@Transactional
public class UserServiceImpl implements UserService { ...}
道:
@Component
@Transactional
public class UserDaoImpl implements UserDao {
JPA配置:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="xxxPersistenceUnit"/>
<property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven />
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么会这样?