一、在SSM框架中,我们可以在applicationContext.xml文件中编写Spring的框架配置
1.头文件
<?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和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="cn.itcast" >
<!--配置哪些注解不扫描-->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
注意:此时开启的注解仅仅是dao和service上的注解,并没有开启web(springmvc)的注解,这里设置了不扫描web的注解
2.编写实体类
package cn.itcast.domain;
import java.io.Serializable;
/**
* 帐户
*/
public class Account implements Serializable{
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
3.编写dao接口
4.编写service接口和实现类
package cn.itcast.service;
import cn.itcast.domain.Account;
import java.util.List;
public interface AccountService {
public void saveAccount(Account account);
public List<Account> findAll();
}
package cn.itcast.service.imp;
import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("accountService")
public class AccountServiceImp implements AccountService {
public void saveAccount(Account account) {
System.out.println("保存了用户信息");
}
public List<Account> findAll() {
System.out.println("查询了所有的用户");
return null;
}
}
5.编写SSM_web项目测试类进行测试
package cn.itcast.test;
import cn.itcast.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceTest {
@Test
public void run1(){
ApplicationContext ac=new
ClassPathXmlApplicationContext("classpath:applicationContext.xml");
AccountService as=(AccountService) ac.getBean("accountService");
as.findAll();
}
}