一、引入jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.neuedu</groupId>
<artifactId>Spring-ch01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Spring-ch01</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>5.0.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
传统方式和spring方式创建对象不同
第一种 实例化类
public class UserService {
public void add(){
System.out.println("add方法被调用");
}
public static void main(String[] args) {
UserService us=new UserService();
us.add();
}
}
第二种 工厂创建
public class UserServiceFactory {
public static UserService getUserServie(){
UserService us=new UserService();
return us;
}
public static void main(String[] args) {
UserService us= UserServiceFactory.getUserServie();
us.add();
}
}
第三种 spring方式
public class UserSpringFactory {
public static UserService getServie()
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
UserService us = null;
// 通过反射创建出对象
Class c = Class.forName("com.neuedu.spring.chap01.UserService");
// 强制转换成UserService
us = (UserService) c.newInstance();
return us;
}
public static void main(String[] args)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
UserService u = UserSpringFactory.getServie();
u.add();
}
}
spring ioc:控制反转(创建对象和给对象设置属性)
1)创建工程-配置依赖(上面已完成)
2)引入配置文件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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
</beans>
3)建立dao service模拟用户登录
IUserDao
public interface IUserDao {
public int login(String username,String pwd);
}
UserDaoImpl
public class UserDaoImpl implements IUserDao {
@Override
public int login(String username, String pwd) {
// TODO Auto-generated method stub
return 0;
}
}
IUserServcie
public interface IUserService {
public int login(String username,String pwd);
}
UserSeriveImpl
public class UserServiceImpl implements IUserService {
private IUserDao userDao;
@Override
public int login(String username, String pwd) {
return userDao.login(username, pwd);
}
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
}
配置文件中加入以下配置
<bean id="userDao" class="com.neuedu.dao.UserDaoImpl"></bean>
<bean id="userService" class="com.neuedu.service.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
测试类:
public class UserTest {
@Test
public void test1(){
//第一步 加载配置文件,获取上下文(新)
//1.1通过加载类路径下的xml来获取上下文
ApplicationContext ctx= new ClassPathXmlApplicationContext("ApplicationContext.xml");
//调用上下文中的方法加载类
IUserService servcie=(IUserService) ctx.getBean("userService");
//调用类里的方法
servcie.login("wang.qj", "123456");
}
}
网友评论