前面的内容讲的是用xml配置Spring,今天就讲讲注解配置。
@Component(组件),@Controller , @Service , @Respository (都是在类上注解)
1.@Component(组件):把类对象在Spring中实例化也就是说让Spring接管这个类,和前边xml配置的作用相同
2.@Controller:对应web层
3.@Service:对应service层
4.@Respository:对应dao层
其实这四个使用起来注解功能是类似的
下面是一个Ioc实例
(1)创建一个xml文件(引入必要的标签),使用Ioc注解必须要配置扫描
(2)新建一个dao和daoimpl
(3)在daoimpl上使用component组件
(4)创建工厂并调用
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring Ioc 使用Ioc注解必须要配置扫描-->
<context:component-scan base-package="testAnno" annotation-config="true"></context:component-scan>
</beans>
下面也使用的利用注解给属性注入值
package testAnno;
public interface PersonDao {
public void tellName();
}
package testAnno;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("PersonDao")
public class PersonDaoImpl implements PersonDao{
// 如果没有set方法就写在属性上边,有set的话写在set上边和属性上边都可以(尽量写在set上)
@Value("魏澳")
private String name;
// @Value("魏澳")
public void setName(String name) {
this.name = name;
}
@Override
public void tellName() {
System.out.println("(PersonDao)hello,I am Wrial!"+name);
}
}
}
属性注入(都是在属性上注解)
1.普通的类型用上述的@value(value="")里边的value可省略
2.对象类型的注入@Autowried,是按照类型完成属性的注入,可以和@Qualifier(value="")组合使用等价于@Resourse(name="")通过Component设置的属性值来注入,因此 @Resourse(name="")用的较多 。
实例分析
package testAnno;
public interface Service {
public void tellName();
}
----------------------------------------------------------------------------------------------------
package testAnno;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import javax.annotation.Resource;
//配置对象实例
@org.springframework.stereotype.Service("Service")
public class ServiceImpl implements Service {
// 注入对象
// @Autowired
// @Qualifier("PersonDao")
@Resource(name = "PersonDao") //name不能省略
public PersonDao personDao;
@Override
public void tellName() {
personDao.tellName();
System.out.println("I aready reach Service!");
}
}
生命周期有关(初始化和销毁)
初始化:PostConstruct等价于xmlBeans配置的init-method
销毁:PreDestroy等价于xmlBeans配置的destroy-method
和前面讲过的一样销毁只存在于单例,原因详情请看Idea搭建Spring和入门案例讲解
@PostConstruct
public void init(){
System.out.println("inti");
}
@PreDestroy
public void destroy(){
System.out.println("destroy");
}
作用范围(Scope)
@Scope(“singleton/prototype/request/session/globalsession”)
scope作用范围和xml中scope一样,其中单例多例最为常用。用法也很简单。
@org.springframework.stereotype.Service("Service")
@Scope("singleton")
public class ServiceImpl implements Service
把这些基本的Ioc注解了解之后,我们来分析分析它的利与弊
xml:结构体系明了,配置不方便,可以配置任何源码
annotation:配置简单快捷,结构必须得查看源码才行,而且只能配置自己写的代码,不能配置别人已经封装好的源码(在SpringBoot可以在类级别配置)
xml+annotation:各取所长,注意在xml+annotation模式下上边xml配置的扫描器context:compotent-scan=""/不能使用在没有注解容器的类里,它扫描的是组件(也就是类上的那些注解)
如果用不用注解配置Bean而用xml配置Bean,若要用注解进行属性注入,那就得使用另外的一个扫描标签context:annotation-config
可以这样说,如果使用了容器包扫描注解就不需要annotation-config注解了,但是如果没有配置包扫描是用的xml配置bean的话,如果要进行属性注入等操作的话必须要加上annotation-config注解。
<!-- spring Ioc 使用Ioc注解必须要配置扫描,扫描容器-->
<context:component-scan base-package="testAnno" ></context:component-scan>
<!--没用注解配置的容器里的方法用下边的标签-->
<context:annotation-config></context:annotation-config>
到现在Ioc基本已经写完,小伙伴对Ioc有没有更加深刻的理解呢。短小的篇幅可以激发出巨大的动力。下一次就说说AOP(面向切面编程)开发。一个模块一个模块的向前进步!