Spring自动扫描
Spring的自动扫描本课是后续使用Spring的基础@Service :用于标注业务层组件,如Service层@Controller :用于标注控制层组件,如Servlet@Repository :用于标注数据访问组件,如DAO层@Resource: 用于为接口注入值。@Component:泛指组件,当组件不好归类的时候,我们可以用这个注解进行标注在com.galaxy.dao包...
·
Spring的自动扫描本课是后续使用Spring的基础
- @Service :用于标注业务层组件,如Service层
- @Controller :用于标注控制层组件,如Controller层
- @Repository :用于标注数据访问组件,如DAO层
- @Resource: 用于为接口注入值。
- @Component:泛指组件,当组件不好归类的时候,我们可以用这个注解进行标注
在com.galaxy.dao包中,新建IPersonDao接口,并提供addPerson()方法
public interface IPersonDao {
public void addPerson();
}
在创建实现类PersonDaoImpl
package com.galaxy.dao.impl;
import com.galaxy.dao.IPersonDao;
import org.springframework.stereotype.Repository;
/*@Repository 相当于:<bean id="personDaoImpl" class="com.galaxy.dao.impl.PersonSDaoImpl"/>
*@Repository("personDao") 相当于:<bean id="personDaoImpl" class="com.galaxy.dao.impl.PersonSDaoImpl"/>
* */
@Repository
public class PersonDaoImpl implements IPersonDao {
@Override
public void addPerson() {
System.out.println("Dao添加Person");
}
}
在com.galaxy.service包中,新建IPersonService接口
public interface IPersonService {
public void addPerson();
}
在com.galaxy.service包中,新建PersonServiceImpl接口,找到addPerson()接口,同时找到实现类,找到 @Resource进行对象得创建,创建完成之后private IPersonDao personDao;就有了值。
package com.galaxy.service.impl;
import com.galaxy.dao.IPersonDao;
import com.galaxy.service.IPersonService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/*
@Service 相当于:<bean id="personService" class="com.galaxy.service.impl.PersonServiceImpl"/>
@Service("personService")相当于:<bean id="personService" class="com.galaxy.service.impl.PersonServiceImpl"/>
*/
@Service
public class PersonServiceImpl implements IPersonService {
/*@Eesource:针对@Repository写法保持一致
*@Repository---@Eesource
* @Repository("personDao)---@Eesource(name = "personDao")
* */
@Resource
private IPersonDao personDao;
@Override
public void addPerson() {
System.out.println("Service调用Dao");
personDao.addPerson();
}
}
核心文件配置
<context:component-scan base-package=“com.galaxy”/>
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启Spring的自动扫描配置 component:组件 scan:扫描、浏览 base-package基础的包-->
<context:component-scan base-package="com.galaxy"/>
</beans>
测试类
package com.galaxy.test;
import com.galaxy.service.impl.PersonServiceImpl;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class testPerson {
@Test
public void Person(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
PersonServiceImpl personService = (PersonServiceImpl) context.getBean("personServiceImpl");
personService.addPerson();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)