Spring dependency injection-Xml way Dao
-(1) Object injection
public class A{
private int id;
private B b;
}
public class XXXService{
private int id;
private XxxDao xxxdao;
}
Test
@Test
public void test09(){
//PersonService personService = new PersonService();
PersonService personService = (PersonService) context.getBean("personService");
Person p = new Person();
p.setUsername("jack");
p.setPassword("12345");
boolean flag =personService.login(p);
log.debug(flag+"");
}
PersonService
public class PersonService {
private static final Logger log= LoggerFactory.getLogger(PersonService.class);
//private PersonDao personDao = new PersonDao();
private PersonDao personDao ;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public boolean login(Person p) {
log.debug(p+" login");
Person person = personDao.find(p);
if(person==null) {
return false;
}else{
return true;
}
}
}
PersonDao
public class PersonDao {
public Person find(Person p) {
if("jack".equals(p.getUsername())&&"12345".equals(p.getPassword())){
return p;
}else{
return null;
}
}
}
applicationContext.xml
<bean id="personService" class="com.wzx.service.PersonService">
<property name="personDao" ref="personDao"/>
</bean>
<bean id="personDao" class="com.wzx.dao.PersonDao">
</bean>
Spring dependency injection-annotation to create objects***
- (1) If there are more objects,
开启注解扫描
<?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.xsd">
<!--
使用注解方式进行创建对象
1.开启注解扫描
含义:开启注解扫描,指定了 base-package 扫描指定的包,扫描包与子包中所有的类
查看类上是否有指定的注解, 如果类上有指定的注解,那么就创建给类对象,
放到spring容器中
-->
<context:component-scan base-package="com.wzx"/>
</beans>
- (2) Only the classes marked with annotations will be created and added to the ioc container
- (3) Four notes
//@Component //其他层
//@Repository //Dao层
//@Service //Service层
@Controller("xxx")//Controller层
public class MyClass{
}
- Annotation does not configure id, but the default is
myClass
@Test
public void test10(){
PersonService personService = (PersonService) context.getBean("personService");
log.debug(personService+" test10");
PersonDao personDao = (PersonDao) context.getBean("personDao");//id为类名首字符小写
log.debug(personDao +" test10");
}
Spring dependency injection-annotation implementation injection***
- (1) What is injection?
After the search, perform the assignment - (2) Three injection methods
"1 ***
@Autowired
or
@Autowired
@Qualifier("bean id")
"2
@Value("#{bean的id}")
"3
@Resource(name="bean id value")
@Service
public class PersonService {
//private PersonDao personDao = new PersonDao();
//第一种:@Autowired或者 @Autowired和@Qualifier("bean的id")搭配
//第二种:@Value("#{bean的id}")
//第三种:@Resource(name="bean的id值")
@Autowired
PersonDao personDao ;
}