<!-- spring-servlet -->
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" |
04 |
xmlns:context = "http://www.springframework.org/schema/context" |
05 |
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
06 |
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd |
07 |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
08 |
http://www.springframework.org/schema/context <a href = "http://www.springframework.org/schema/context/spring-context-3.0.xsd" >http://www.springframework.org/schema/context/spring-context-3.0.xsd</ a >"> |
11 |
< context:annotation-config /> |
14 |
< context:component-scan base-package = "controller" ></ context:component-scan > |
17 |
< bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> |
20 |
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix = "/jsp/" p:suffix = ".jsp" /> |
<!-- applicationContext.xml -->
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
04 |
xmlns:aop = "http://www.springframework.org/schema/aop" |
05 |
xmlns:tx = "http://www.springframework.org/schema/tx" |
07 |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
08 |
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
09 |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> |
12 |
< bean id = "sessionFactory" class = "org.springframework.orm.hibernate3.LocalSessionFactoryBean" > |
13 |
< property name = "configLocation" > |
14 |
< value >classpath:config/hibernate.cfg.xml</ value > |
19 |
< bean id = "transactionManager" class = "org.springframework.orm.hibernate3.HibernateTransactionManager" > |
20 |
< property name = "sessionFactory" > |
21 |
< ref local = "sessionFactory" /> |
26 |
< tx:annotation-driven transaction-manager = "transactionManager" proxy-target-class = "true" /> |
29 |
< bean id = "loginService" class = "service.LoginService" ></ bean > |
32 |
< bean id = "hibernateDao" class = "dao.HibernateDao" > |
33 |
< property name = "sessionFactory" ref = "sessionFactory" ></ property > |
注解方式:
1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
下面写这个是引入component的扫描组件
<context:component-scan base-package=”com.mmnc”>
@autowired与@Resource 区别
@Autowired默认按类型装配(这个注解是属业spring的),默认情况下必须要求依赖对象必须存在,如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) ,如果我们想使用名称装配可以结合@Qualifier注解进行使用,如下:
- @Autowired() @Qualifier("baseDao")
- private BaseDao baseDao;
3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定,
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。
扫描二维码关注公众号,回复:
371099 查看本文章
- @Resource(name="baseDao")
- private BaseDao baseDao;
其中base-package为需要扫描的包(含所有子包)
1、@Service用于标注业务层组件
2、@Controller用于标注控制层组件(如struts中的action)
3、@Repository用于标注数据访问组件,即DAO组件.
4、@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Service public class UserServiceImpl implements UserService { }
@Repository public class UserDaoImpl implements UserDao { } getBean的默认名称是类名(头字母小写),如果想自定义,可以@Service(“***”) 这样来指定,这种bean默认是单例的,如果想改变,可以使用@Service(“beanName”)
@Scope(“prototype”)来改变。可以使用以下方式指定初始化方法和销毁方法(方法名任意): @PostConstruct public void init() { }