各位大大,请问个问题:我在持久层、业务层和控制层中分别使用
@Repository/@Service/@Controller
这样意义和直接全部使用@Component有什么区别呢?
兄弟 那说明我刚才给你说的那个配置 应该是没问题的 这个问题应该是解决了的。
[quote]
@Repository/@Service/@Controller
这样意义和直接全部使用@Component有什么区别呢?
[/quote]
没什么差别,差别只是在语义上,@Repository/@Service/@Controller分别代表了特定语义的类,这个有点类似于HTML 5提出的语义化标签,你说HTML 5里面的“header”和“div”有什么差别呢,其实功能上来说没有,只是语义表达的更清楚。
@Repository、@Service 和 @Controller。在目前的 Spring 版本中,和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然[color=red]目前这 3 个注释和 @Component 相比没有什么新意[/color],[color=blue]但 Spring 将在以后的版本中可能会为它们添加特殊的功能[/color]。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository、@Service 和 @Controller 对分层中的类进行注释,而用 @Component 对那些比较中立的类进行注释。
ls正解
[quote]
嗯那么我在DAO和SERVICE中该用哪个注解呢?都是@Service么?
因为我比较菜刚开始认识注入,还有就是现在注入遇到点问题,我的Application-context.xml中配置了:
然后Action配置了@Controller/Service和DAO配置了@Service/实体类里面配置了@Repository,为什么Action中我用@Autowired注入Service的时候还是没有注入进去呢就是一使用Service就报NullPointException,Service是空的。
[/quote]
DAO,一般用@Repository,它这一层是数据的入口和出口,对数据库最基本的操作,有时候我们注解这个类,更多的是对它有个数据库操作的管理,比如你继承了spring自己的hibernateDAo这样的东西,那怎么给你注入一些数据库操作的元素了,就靠它了,所以这个不是随便注释的。
而service采用@service注解,因为service我们知道,需要对事务进行控制对吧,那么怎么办到了,肯定得统一用注解来标识,才能办到。
你那种配置方法配置错了。
标准配置方法是:
pojo:配置
@Entity
@Table(name = "ACTE_BMSZ", schema = "HB_TALENTS")
dao:配置
@Repository
service配置@Component
你的applicationContext.xml里面需要配置
[code="java"][/code]
才能完成自动注入
org.yourpackage是你统一放的java文件
[quote]
为什么Action中我用@Autowired注入Service的时候还是没有注入进去呢就是一使用Service就报NullPointException,Service是空的。
[/quote]
要使用@Autowired需要开启下面的配置:
[code="java"]context:annotation-config/[/code]
你加了没有?
你需要加上context的命名空间就可以了,像这样:
[code="java"]
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
[/code]
还有就好就是你所有声明Bean的注解(@Repository、@Service 和 @Controller )的类都必须在org.yourpackage包下面。
[code="java"]context:annotation-config/ [/code]
这个可以不需要,因为[code="java"][/code]已经隐含激活了。
[quote]
Note: This tag implies the effects of the 'annotation-config' tag, activating
@Required, @Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and
@PersistenceUnit annotations in the component classes, which is usually desired for autodetected
components (without external configuration).
[/quote]
应该是其他的问题,你是怎么注入Service,贴出来看看。
另外你可以通过spring日志看看你定义的Service,dao等类是否都创建了,因为默认情况下单例Bean不会延迟创建的。如果你通过注解声明的Bean都创建了,那就是注入Bean的地方出错了。
[code="java"]
@Autowired
private UserInfo userInfo;
[/code]
貌似这个东西注入是多余的吧
[quote]这样还是抛出控指针异常,并提示UserInfoService为Null[/quote]
这个个说明你注入不成功。你可以尝试在声明Service采用:
[code="java"]
@Service("myMovieFinder")
public class MovieFinder {
// ...
}
[/code]
这样在Action注入是通过Resource引用名字来注入:
[code="java"]
public class SimpleMovieLister {
@Resource(name="myMovieFinder")
private MovieFinder movieFinder;
}
[/code]
类似这样的做法。
[code="xml"]
contextConfigLocation
classpath*:/resources/applicationContext.xml
[/code]
改成这样的试试
[quote]尝试了一下。。依旧。。您说会不会是我的applicationContext没有识别到呢?。。。[/quote]
有可能,你从日志上能看到你的Service被创建了吗?
你的日志已经说明问题了,正常情况下应该有下面的日志:
[code="java"]
Bean factory for Root WebApplicationContext: org.springframework.beans.factory.support.DefaultListableBeanFactory@1f8d0a4: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,accountManager,userDao,roleDao,authorityDao,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,dataSource,sessionFactory,transactionManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org.springframework.security.web.PortMapperImpl#0,org.springframework.security.web.context.HttpSessionSecurityContextRepository#0,org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy#0,org.springframework.security.authentication.ProviderManager#0,org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler#0,org.springframework.security.access.vote.AffirmativeBased#0,org.springframework.security.web.access.intercept.FilterSecurityInterceptor#0,org.springframework.security.web.access.DefaultWebInvocationPrivilegeEvaluator#0,org.springframework.security.authentication.AnonymousAuthenticationProvider#0,org.springframework.security.web.savedrequest.HttpSessionRequestCache#0,org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint#0,org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#0,org.springframework.security.config.http.UserDetailsServiceInjectionBeanPostProcessor#0,org.springframework.security.filterChainProxy,org.springframework.security.authentication.dao.DaoAuthenticationProvider#0,org.springframework.security.authentication.DefaultAuthenticationEventPublisher#0,org.springframework.security.authenticationManager,userDetailsService]; root of factory hierarchy
[/code]
但现在你的日志里面显示:
[code="java"]
org.springframework.beans.factory.support.DefaultListableBeanFactory@13c6a22: defining beans []; root of factory hierarchy
[/code]
spring根本没有发现bean的定义。是不是配置文件还是有问题。
我看你已经把spring的日志级别开到INFO了,但是却没有看到加载xml相关的日志。正常情况应该有:
[code="java"]
2011-08-24 17:29:53,250 [main] INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader] - Loading XML bean definitions from URL [file:/D:/TDDOWNLOAD/springside-3.3.4/examples/mini-web/target/classes/applicationContext.xml]
[/code]
类似于这样的日志啊。
绝对是配置文件的路径有问题。我猜想你把resource也和src是配成一个的。
[code="xml"]
contextConfigLocation
classpath*:/applicationContext.xml
[/code]
你改成我这样 应该没问题了。
这次保证没问题了。
在你的web-inf下看看,到底你的spring配置文件在哪里,然后再在web.xml上配置好。实在不行,直接把配置移到classes里面。
我刚才之所以让你 前面加个resource这个东西 是因为你说是同目录,后来发觉肯定是你们头把这个目录和src目录都可以被映射成同一级 相对目录,所以让你改成这样。
应该没问题了。