HelloSpring
1. 导入Jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
2. 新建Hello类
新建Hello类,新建SET和GET数据和String数据。
package cn.edu.jxust.pojo;
public class Hello {
public String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
3. 新建beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring这些都称为Bean
类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置值
-->
<bean id="hello" class="cn.edu.jxust.pojo.Hello">
<property name="str" value="Spring" />
</bean>
</beans>
4. 测试
import cn.edu.jxust.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//获取Spring的上下文对象!
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在Spring中的管理了,我们要使用,直接去里面取出来就可以!
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString());
}
}
IOC理论推导
原案例
1. dao层
package cn.edu.jxust.dao;
public interface UserDao {
void getUser();
}
package cn.edu.jxust.dao.impl;
import cn.edu.jxust.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void getUser() {
System.out.println("获得用户数据");
}
}
package cn.edu.jxust.dao.impl;
import cn.edu.jxust.dao.UserDao;
public class UserDaoMysqlImpl implements UserDao {
@Override
public void getUser() {
System.out.println("使用MySQL获取数据");
}
}
package cn.edu.jxust.dao.impl;
import cn.edu.jxust.dao.UserDao;
public class UserDaoOrcleImpl implements UserDao {
@Override
public void getUser() {
System.out.println("Oracle获取用户数据");
}
}
2. service层
package cn.edu.jxust.service;
public interface UserService {
void getUser();
}
package cn.edu.jxust.service.impl;
import cn.edu.jxust.dao.UserDao;
import cn.edu.jxust.dao.impl.UserDaoImpl;
import cn.edu.jxust.service.UserService;
public class UserServiceImpl implements UserService {
UserDao userDao = new UserDaoImpl();
@Override
public void getUser() {
userDao.getUser();
}
}
3. 输出结果
import cn.edu.jxust.service.UserService;
import cn.edu.jxust.service.impl.UserServiceImpl;
public class MyTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层他们不需要接触!
UserService userService = new UserServiceImpl();
userService.getUser();
}
}
完善案例
1. 修改
import cn.edu.jxust.dao.UserDao;
import cn.edu.jxust.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
//利用set进行动态实现值的注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void getUser() {
userDao.getUser();
}
}
2. 输出结果
public class MyTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层他们不需要接触!
UserService userService = new UserServiceImpl();
((UserServiceImpl) userService).setUserDao(new UserDaoOrcleImpl());
userService.getUser();
}
}
使用beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="sqlImpl" class="cn.edu.jxust.dao.impl.UserDaoImpl" />
<bean id="mysqlImpl" class="cn.edu.jxust.dao.impl.UserDaoMysqlImpl" />
<bean id="oracleImpl" class="cn.edu.jxust.dao.impl.UserDaoOrcleImpl" />
<bean id="UserServiceImpl" class="cn.edu.jxust.service.impl.UserServiceImpl">
<!--
ref:引用Spring容器中创建好的对象
value: 具体的值,基本数据类型
-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>
import cn.edu.jxust.dao.impl.UserDaoOrcleImpl;
import cn.edu.jxust.service.UserService;
import cn.edu.jxust.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
//用户实际调用的是业务层,dao层他们不需要接触!
// UserService userService = new UserServiceImpl();
// ((UserServiceImpl) userService).setUserDao(new UserDaoOrcleImpl());
// userService.getUser();
//获取ApplicaitonContext:拿到Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//容器在手,天下我有,需要什么,g直接get什么!
UserServiceImpl userService = (UserServiceImpl) context.getBean("UserServiceImpl");
userService.getUser();
}
}