ssm框架整合spring
1.配置applicationContext.xml
2.配置组件扫描
3.创建业务类、接口类以及测试类
4.测试验证spring添加是否成功
- applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 设置扫描包,包下设置注解@Service @Repository @Component @AutoWried-->
<context:component-scan base-package="com.wzx">
<!-- 由于springmvc的controller是由springmvc来扫描,需要将controller排除在外-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
</beans>
- 编写一个实体类Person
public class Person {
private Integer id;
private String name;
private Double money;
@Override
public String toString() {
return "{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
public Person( String name, Double money) {
this.name = name;
this.money = money;
}
public Person() {
}
}
- 创建业务接口IPersonService
public interface IPersonService {
//显示所有的person数据
List<Person> findAll();
//保存一个person数据
void savePerson(Person person);
}
- 创建业务接口实现类PersonServiceImpl
@Service
public class PersonServiceImpl implements IPersonService {
@Autowired
private IPersonDao personDao;
@Override
public List<Person> findAll() {
List<Person> list=personDao.findAll();
return list;
}
@Override
public void savePerson(Person person) {
personDao.save(person);
}
- 创建接口IPersonDao
public interface IPersonDao {
List<Person> findAll();
void save(Person person);
}
- 创建接口实现类PersonDaoImpl
@Repository
public class PersonDaoImpl implements IPersonDao {
@Override
public List<Person> findAll() {
System.out.println("findAll dao");
return null;
}
@Override
public void save(Person person) {
System.out.println("save dao");
}
}
- 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestPersonService {
private static final Logger log=Logger.getLogger(TestPersonService.class);
@Autowired
IPersonService personService;
@Test
public void test01(){
log.debug(personService);
List<Person> list=personService.findAll();
log.debug(list);
Person person=new Person("dsf",200.00);
System.out.println(person);
personService.savePerson(person);
List<Person> list1=personService.findAll();
log.debug(list1);
}
@Test
public void test02(){
System.out.println(personService);
Person person=new Person("dsf",200.00);
System.out.println(person);
}
}