1.首先先看一整个项目的目录结构及所用的包。
2.IOC控制反转 + DI依赖注入 将对象通过spring的核心配置注入到spring容器中 使用容器创建对象,不需要自己new,降低耦合性
3.话不多说,直接上代码
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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 https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 通过bean元素声明所需要的spring创建对象 id:创建的对象名 class:需要创建类对象的类路径 name:对象的属性名-->
<!--扫描包中注解标注的类文件-->
<!-- context:component-scan:扫描注解 base-package:需要扫描的包的路径-->
<context:component-scan base-package="com.yang.dao,com.yang.service"></context:component-scan>
</beans>
entity类
package com.yang.entity;
public class Address {
private Integer id;
private String contact;
private String addressDesc;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getAddressDesc() {
return addressDesc;
}
public void setAddressDesc(String addressDesc) {
this.addressDesc = addressDesc;
}
}
dao接口类
package com.yang.dao;
import com.yang.entity.Address;
public interface AddressDao {
public void addAddress(Address address);
}
dao实现类
package com.yang.dao;
import com.yang.entity.Address;
import org.springframework.stereotype.Repository;
@Repository //注解实现 dao层类组件
public class AddressDaoImpl implements AddressDao{
@Override
public void addAddress(Address address) {
System.out.println("模拟数据库增删改查,新增成功!!!");
}
}
service接口类
package com.yang.service;
import com.yang.entity.Address;
public interface AddressService {
public void addAddress(Address address);
}
service实现类
package com.yang.service;
import com.yang.dao.AddressDao;
import com.yang.entity.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("service")
public class AddressServiceImpl implements AddressService{
//直接在对象的属性上自动装配
@Autowired
private AddressDao addressDao;
//通过set方法注入
// @Autowired
// public void setAddressService(AddressService addressService) {
// this.addressService = addressService;
// }
@Override
public void addAddress(Address address) {
addressDao.addAddress(address);
}
}
测试类
package com.yang.test;
import com.yang.entity.Address;
import com.yang.service.AddressService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAddress {
@Test
public void testAdd(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AddressService addressService = (AddressService) context.getBean("service");
Address address = new Address();
address.setId(1);
address.setContact("zhangsan");
address.setAddressDesc("北京市东城区");
addressService.addAddress(address);
}
}