一、@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;
…
}
我们还得配置文件进行配置:
这样就把com.zxr.manager包下的所有.*DaoImpl,.*ServiceImpl都注册关联到Spring容器中去了
它是伪代码,表示重写(当然不写也可以),不过用它有以下好处:
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容器中去了