什么是注解
传统的spring做法是使用.xml文件来对bean进行注入或者是配置aop、事物,这么做有两个缺点:
1、如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大;如果按需求分开.xml文件,那么.xml文件又会非常多。总之这将导致配置文件的可读性与可维护性变得很低
2、在开发中在.Java文件和.xml文件之间不断切换,是一件麻烦的事,同时这种思维上的不连贯也会降低开发的效率
为了解决这两个问题,Spring引入了注解,通过”@XXX”的方式,让注解与Java Bean紧密结合,既大大减少了配置文件的体积,又增加了java Bean的可读性与内聚性。
本篇文章,讲讲最重要的三个Spring注解,也就是@Autowired、@Resource和@Service,希望能通过有限的篇幅说清楚这三个注解的用法。
一、目录结构
2、Zoo.java
package com.itheima.z_ioc.autowired;
import org.springframework.beans.factory.annotation.Autowired;
public class Zoo
{
private Tiger tiger;
private Monkey monkey;
public void setTiger(Tiger tiger)
{
this.tiger = tiger;
}
public void setMonkey(Monkey monkey)
{
this.monkey = monkey;
}
public Tiger getTiger()
{
return tiger;
}
public Monkey getMonkey()
{
return monkey;
}
public String toString()
{
return tiger + "\n" + monkey;
}
}
3、Tiger.java
package com.itheima.z_ioc.autowired;
public class Tiger
{
private String tigerName = "TigerKing";
public String toString()
{
return "TigerName:" + tigerName;
}
}
4、Monkey.java
package com.itheima.z_ioc.autowired;
public class Monkey
{
private String monkeyName = "MonkeyKing";
public String toString()
{
return "MonkeyName:" + monkeyName;
}
}
5、配置文件 beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byType">
<bean id="zoo" class="com.xrq.bean.Zoo" >
<property name="tiger" ref="tiger" />
<property name="monkey" ref="monkey" />
</bean>
<bean id="tiger" class="com.itheima.z_ioc.autowired.Tiger" />
<bean id="monkey" class="com.itheima.z_ioc.autowired.Monkey" />
</beans>
6、测试程序
package com.itheima.z_ioc.autowired;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Testautowired {
@Test
public void demo02(){
//从spring容器获得
//1 获得容器
String xmlPath = "com/itheima/z_ioc/autowired/beans.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
//2获得内容 --不需要自己new,都是从spring容器获得
Zoo zoo = (Zoo) applicationContext.getBean("zoo");
System.out.println(zoo.toString());
}
}
—————————————————-
改造之后 用Autowired
1、Zoo.java 【如果还是以前的代码,测试程序将是 两个null】
package com.itheima.z_ioc.autowired;
import org.springframework.beans.factory.annotation.Autowired;
public class Zoo
{
@Autowired
private Tiger tiger;
@Autowired
private Monkey monkey;
public String toString()
{
return tiger + "\n" + monkey;
}
}
2、配置文件beans.xml
<?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: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-3.2.xsd">
<!-- default-autowire="byType">
<bean id="zoo" class="com.itheima.z_ioc.autowired.Zoo" >
<property name="tiger" ref="tiger" />
<property name="monkey" ref="monkey" />
</bean>
<bean id="tiger" class="com.itheima.z_ioc.autowired.Tiger" />
<bean id="monkey" class="com.itheima.z_ioc.autowired.Monkey" /> -->
<context:component-scan base-package="com.itheima.z_ioc.autowired" />
<bean id="zoo" class="com.itheima.z_ioc.autowired.Zoo" />
<bean id="tiger" class="com.itheima.z_ioc.autowired.Tiger" />
<bean id="monkey" class="com.itheima.z_ioc.autowired.Monkey" />
</beans>
注:
<bean id="zoo" class="com.itheima.z_ioc.autowired.Zoo" />
<bean id="tiger" class="com.itheima.z_ioc.autowired.Tiger" />
<bean id="monkey" class="com.itheima.z_ioc.autowired.Monkey" />
还是要有这个,而不是自动注入。只是说他们之间的相对关系取消了
———————————————————————————————————
第三部分 @Service
@Service注解,其实做了两件事情:
1、声明Zoo.java是一个bean,这点很重要,因为Zoo.java是一个bean,其他的类才可以使用@Autowired将Zoo作为一个成员变量自动注入
2、Zoo.java在bean中的id是”zoo”,即类名且首字母小写