Spring2.5中annotation配置中@Override、@Component、 @Controller、 @Service、 @Repository、@Autowired的解析

一、@Override
它是伪代码,表示重写(当然不写也可以),不过用它有以下好处:
1、可以当注释用,方便阅读;
2、编译器可以给你验证@Override下面的方法名是否是你父类中所有的,如果没有则报错。例如,你如果没写@Override,而你下面的方法名又写错了,这时你的编译器是可以编译通过的,因为编译器以为这个方法是你的子类中自己增加的方法。


关于第二种优点的举例:
在重写父类的onCreate时,在方法前面加上@Override 系统可以帮你检查方法的正确性。
@Override
public void onCreate(Bundle savedInstanceState)
{…….}
这种写法是正确的,如果你写成:
@Override
public void oncreate(Bundle savedInstanceState)
{…….}
编译器会报如下错误:The method oncreate(Bundle) of type HelloWorld must override or implement a supertype method,以确保你正确重写onCreate方法(因为oncreate应该为onCreate)。而如果你不加@Override,则编译器将不会检测出错误,而是会认为你为子类定义了一个新方法:oncreate。


二、@Component
它表示一个通用注释用于说明一个类是一个spring容器管理的类,即该类已经拉入到spring的管理中了。(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
举例:
<context:component-scan base-package=”com.mmnc”>    
其中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() { } 


三、@Controller
它是对@Component的细化的类。(注入服务)


四、@Service
它是对@Component的细化,用来给服务层的类定义一个名字,让Spring根据这个名字关联到这个类。(注入dao)
举例:
Chinese实例化为chinese,如果需要自己改名字则:@Service("你自己改的bean名")。
   


五、@Repository
它是对@Component的细化,用来给持久层的类定义一个名字,让Spring根据这个名字关联到这个类。(实现dao访问)
例如:
@Repository("userDao")
public class UserDaoImpl  implements UserDao{
   ........................................
}
声明了UserDaoImpl  在Spring容器中叫userDao这个名字。


六、@Autowired
它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
举例:
package com.baobaotao;   
import org.springframework.beans.factory.annotation.Autowired;   
 
public class Boss {   
    @Autowired  
    private Car car;   
  
    @Autowired  
    private Office office;   
    …   
}

我们还得配置文件进行配置:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:annotation-config/>
    <context:component-scan base-package="com.zxr.manager">
        <context:include-filter type="regex" expression=".*DaoImpl"/>
        <context:include-filter type="regex" expression=".*ServiceImpl"/>
    </context:component-scan>
</beans>


这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果要使用@Resource注解实现装配,该如何修改代码package org.example; public interface StudentDao { void list(); void delete(int id); } package org.example; public interface StudentService { void list(); void delete(int id); } package org.example; import org.springframework.stereotype.Repository; @Repository("mockStudentDao") public class MockStudentDao implements StudentDao{ @Override public void list() { System.out.println("All students are listed in MockStudentDao."); } @Override public void delete(int id) { System.out.println("Student No."+id+"is deleted in MockStudentDao."); } } package org.example; import org.springframework.stereotype.Repository; @Repository("myBatisStudentDao") public class MyBatisStudentDao implements StudentDao{ @Override public void list() { System.out.println("All students are listed in MyBatisStudentDao."); } @Override public void delete(int id) { System.out.println("Student No."+id+"is deleted in MyBatisStudentDao."); } } package org.example; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; @Service("studentServiceImpl") public class StudentServiceImpl implements StudentService{ private final StudentDao studentDao; @Autowired public StudentServiceImpl(@Qualifier("myBatisStudentDao")StudentDao studentDao){ this.studentDao=studentDao; } @Override public void list() { this.studentDao.list(); } @Override public void delete(int id) { this.studentDao.delete(id); } } package org.example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); StudentService studentService = (StudentService) ctx.getBean("studentServiceImpl"); studentService.list(); studentService.delete(10); } }如果要使用MockStudentDao
05-17

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值