spring 配置

 <!-- 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>">
09 
10     <!-- 启用spring mvc 注解 -->
11     <context:annotation-config />
12 
13     <!-- 设置使用注解的类所在的jar包 -->
14     <context:component-scan base-package="controller"></context:component-scan>
15 
16     <!-- 完成请求和注解POJO的映射 -->
17     <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
18 
19     <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
20     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/jsp/" p:suffix=".jsp" />
21 </beans>

 

 

 

<!-- 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"
06         xsi:schemaLocation="
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">
10 
11     <!-- 采用hibernate.cfg.xml方式配置数据源 -->
12     <bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
13         <property name="configLocation">
14             <value>classpath:config/hibernate.cfg.xml</value>
15         </property>
16     </bean>
17      
18     <!-- 将事务与Hibernate关联 -->
19     <bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
20         <property name="sessionFactory">
21             <ref local="sessionFactory"/>
22         </property>
23     </bean>
24      
25     <!-- 事务(注解 )-->
26     <tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true"/>
27 
28    <!-- 测试Service -->
29    <bean id="loginService" class="service.LoginService"></bean>
30 
31     <!-- 测试Dao -->
32     <bean id="hibernateDao" class="dao.HibernateDao">
33         <property name="sessionFactory" ref="sessionFactory"></property>
34     </bean>
35 </beans>

 

 

注解方式:

 

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注解进行使用,如下: 

Java代码   收藏代码
  1. @Autowired() @Qualifier("baseDao")     
  2. private BaseDao baseDao;    

 3、@Resource(这个注解属于J2EE的),默认安照名称进行装配,名称可以通过name属性进行指定, 
如果没有指定name属性,当注解写在字段上时,默认取字段名进行按照名称查找,如果注解写在setter方法上默认取属性名进行装配。 当找不到与名称匹配的bean时才按照类型进行装配。但是需要注意的是,如果name属性一旦指定,就只会按照名称进行装配。

Java代码   收藏代码
  1. @Resource(name="baseDao")     
  2. 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() { } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值