一.bean的注解
要只用注解,必须要开启注解驱动
(一)Resource注解
Resource注解既可以加在属性上面,也可以记在set方法上面
1.Resource注解加在属性上面
package com.csii.dao;
public interface UserDao {
public void say();
}
package com.csii.dao.impl;
import com.csii.dao.UserDao;
public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("say.........");
}
}
package com.csii.service.impl;
import javax.annotation.Resource;
import com.csii.dao.UserDao;
public class ServiceTest {
@Resource
private UserDao userdao;
public void hello() {
System.out.println("hello............");
userdao.say();
}
}
package com.csii.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.csii.service.impl.ServiceTest;
class Test1 {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml");
@Test
void test() {
ServiceTest st=(ServiceTest) ac.getBean("service");
st.hello();
}
}
十月 28, 2018 10:06:17 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a765367: startup date [Sun Oct 28 10:06:17 CST 2018]; root of context hierarchy
十月 28, 2018 10:06:17 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean1.xml]
hello............
say.........
说明:Resource注解的第一匹配原则是根据name匹配,默认情况下name是所注解的属性名,如果配置文件当中的id与name匹配不上,就会根据type来匹配,如果在匹配不上就会报错,如果Resource(name="userdao1")即指定了name,则在匹配不到的情况下不会根据type匹配会直接报错。
2.Resource注解加载set方法上面
此时不需要提供set方法
package com.csii.service.impl;
import javax.annotation.Resource;
import com.csii.dao.UserDao;
public class ServiceTest {
//@Resource
private UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
@Resource
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
public void hello() {
System.out.println("hello............");
userdao.say();
}
}
十月 28, 2018 10:16:49 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a765367: startup date [Sun Oct 28 10:16:49 CST 2018]; root of context hierarchy
十月 28, 2018 10:16:49 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean1.xml]
hello............
say.........
默认不指定@Resource的name的时候,bean的id和如果下左边的三个框内的任意一个名称能匹配上就能注入。如果三个名字都匹配不上就会按着接口和实现类的关系来匹配,如果遇到两个实现类就会报错,这时要求@Resource的name
(二)Autowired注解
1.加在属性上
package com.csii.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.csii.dao.UserDao;
public class ServiceTest {
@Autowired
private UserDao userdao;
public void hello() {
System.out.println("hello............");
userdao.say();
}
}
十月 28, 2018 3:35:19 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a765367: startup date [Sun Oct 28 15:35:19 CST 2018]; root of context hierarchy
十月 28, 2018 3:35:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean1.xml]
hello............
say.........
@Autowired按着接口和实现类的关系来匹配的,如果存在多个接口的实现类的时候,会找和定义的接口名字相同的bean,如果都不相同会报错,故我们必须要指定name来匹配,要结合@Qulifier的注解来指定value和bean的id匹配来注入。
/**
* 在使用Autowried注入的时候,可以设置required=false,则表示可以允许注入的为空
* 但是因为注入的为空,所以下面的式子会报错NullPointerException
*/
2.加在set方法上
package com.csii.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.csii.dao.UserDao;
public class ServiceTest {
//@Autowired
private UserDao userdao;
public UserDao getUserdao() {
return userdao;
}
@Autowired
@Qualifier(value="userdao1")
public void setUserdao(UserDao userdao) {
this.userdao = userdao;
}
public void hello() {
System.out.println("hello............");
userdao.say();
}
}
package com.csii.dao.impl;
import com.csii.dao.UserDao;
public class UserDaoImpl1 implements UserDao{
@Override
public void say() {
System.out.println("say1.........");
}
}
十月 28, 2018 3:41:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a765367: startup date [Sun Oct 28 15:41:40 CST 2018]; root of context hierarchy
十月 28, 2018 3:41:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean1.xml]
hello............
say1.........
三.spring的扫描器管理Bean
实际项目如果很大就会出现大量配置,我们使用扫描器可以解决这个问题,扫描器扫描带有@Controller,@Service, @Repository,@Component的类
@Controller:控制层的类
@Service:服务层的类
@Repository:数据层的类
@Component:无法分层的类上
以上注解标注的类的bean的id默认类名的首字符小写####
package com.csii.dao.impl;
import org.springframework.stereotype.Service;
import com.csii.dao.UserDao;
@Service
public class UserDaoImpl implements UserDao{
@Override
public void say() {
System.out.println("say.........");
}
}
package com.csii.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.csii.dao.UserDao;
@Service
public class ServiceTest {
@Autowired
private UserDao userdao;
public void hello() {
System.out.println("hello............");
userdao.say();
}
}
package com.csii.test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.csii.service.impl.ServiceTest;
class Test1 {
ApplicationContext ac=new ClassPathXmlApplicationContext("bean1.xml");
@Test
void test() {
ServiceTest st=(ServiceTest) ac.getBean("serviceTest");
st.hello();
}
}
十月 28, 2018 4:01:32 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7a765367: startup date [Sun Oct 28 16:01:32 CST 2018]; root of context hierarchy
十月 28, 2018 4:01:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean1.xml]
hello............
say.........
注意:
@Autowired
private UserDao userdao;
不加注解的时候,在调用
public void hello() {
System.out.println("hello............");
userdao.say();
}
不会报错,但是userdao.say()不起作用