HelloSpring

1. 导入Jar包

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-webmvc</artifactId>
  4. <version>5.1.10.RELEASE</version>
  5. </dependency>

2. 新建Hello类

新建Hello类,新建SET和GET数据和String数据。

  1. package cn.edu.jxust.pojo;
  2. public class Hello {
  3. public String str;
  4. public String getStr() {
  5. return str;
  6. }
  7. public void setStr(String str) {
  8. this.str = str;
  9. }
  10. @Override
  11. public String toString() {
  12. return "Hello{" +
  13. "str='" + str + '\'' +
  14. '}';
  15. }
  16. }

3. 新建beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--使用Spring来创建对象,在Spring这些都称为Bean
  7. 类型 变量名 = new 类型();
  8. Hello hello = new Hello();
  9. id = 变量名
  10. class = new 的对象
  11. property 相当于给对象中的属性设置值
  12. -->
  13. <bean id="hello" class="cn.edu.jxust.pojo.Hello">
  14. <property name="str" value="Spring" />
  15. </bean>
  16. </beans>

4. 测试

  1. import cn.edu.jxust.pojo.Hello;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MyTest {
  5. public static void main(String[] args) {
  6. //获取Spring的上下文对象!
  7. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  8. //我们的对象现在都在Spring中的管理了,我们要使用,直接去里面取出来就可以!
  9. Hello hello = (Hello) context.getBean("hello");
  10. System.out.println(hello.toString());
  11. }
  12. }

image.png

IOC理论推导

原案例

1. dao层

  1. package cn.edu.jxust.dao;
  2. public interface UserDao {
  3. void getUser();
  4. }
  1. package cn.edu.jxust.dao.impl;
  2. import cn.edu.jxust.dao.UserDao;
  3. public class UserDaoImpl implements UserDao {
  4. @Override
  5. public void getUser() {
  6. System.out.println("获得用户数据");
  7. }
  8. }
  1. package cn.edu.jxust.dao.impl;
  2. import cn.edu.jxust.dao.UserDao;
  3. public class UserDaoMysqlImpl implements UserDao {
  4. @Override
  5. public void getUser() {
  6. System.out.println("使用MySQL获取数据");
  7. }
  8. }
  1. package cn.edu.jxust.dao.impl;
  2. import cn.edu.jxust.dao.UserDao;
  3. public class UserDaoOrcleImpl implements UserDao {
  4. @Override
  5. public void getUser() {
  6. System.out.println("Oracle获取用户数据");
  7. }
  8. }

2. service层

  1. package cn.edu.jxust.service;
  2. public interface UserService {
  3. void getUser();
  4. }
  1. package cn.edu.jxust.service.impl;
  2. import cn.edu.jxust.dao.UserDao;
  3. import cn.edu.jxust.dao.impl.UserDaoImpl;
  4. import cn.edu.jxust.service.UserService;
  5. public class UserServiceImpl implements UserService {
  6. UserDao userDao = new UserDaoImpl();
  7. @Override
  8. public void getUser() {
  9. userDao.getUser();
  10. }
  11. }

3. 输出结果

  1. import cn.edu.jxust.service.UserService;
  2. import cn.edu.jxust.service.impl.UserServiceImpl;
  3. public class MyTest {
  4. public static void main(String[] args) {
  5. //用户实际调用的是业务层,dao层他们不需要接触!
  6. UserService userService = new UserServiceImpl();
  7. userService.getUser();
  8. }
  9. }

image.png

完善案例

1. 修改

  1. import cn.edu.jxust.dao.UserDao;
  2. import cn.edu.jxust.service.UserService;
  3. public class UserServiceImpl implements UserService {
  4. private UserDao userDao;
  5. //利用set进行动态实现值的注入
  6. public void setUserDao(UserDao userDao) {
  7. this.userDao = userDao;
  8. }
  9. @Override
  10. public void getUser() {
  11. userDao.getUser();
  12. }
  13. }

2. 输出结果

  1. public class MyTest {
  2. public static void main(String[] args) {
  3. //用户实际调用的是业务层,dao层他们不需要接触!
  4. UserService userService = new UserServiceImpl();
  5. ((UserServiceImpl) userService).setUserDao(new UserDaoOrcleImpl());
  6. userService.getUser();
  7. }
  8. }

使用beans.xml完善

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!-- services -->
  7. <bean id="sqlImpl" class="cn.edu.jxust.dao.impl.UserDaoImpl" />
  8. <bean id="mysqlImpl" class="cn.edu.jxust.dao.impl.UserDaoMysqlImpl" />
  9. <bean id="oracleImpl" class="cn.edu.jxust.dao.impl.UserDaoOrcleImpl" />
  10. <bean id="UserServiceImpl" class="cn.edu.jxust.service.impl.UserServiceImpl">
  11. <!--
  12. ref:引用Spring容器中创建好的对象
  13. value: 具体的值,基本数据类型
  14. -->
  15. <property name="userDao" ref="mysqlImpl"/>
  16. </bean>
  17. </beans>
  1. import cn.edu.jxust.dao.impl.UserDaoOrcleImpl;
  2. import cn.edu.jxust.service.UserService;
  3. import cn.edu.jxust.service.impl.UserServiceImpl;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class MyTest {
  7. public static void main(String[] args) {
  8. //用户实际调用的是业务层,dao层他们不需要接触!
  9. // UserService userService = new UserServiceImpl();
  10. // ((UserServiceImpl) userService).setUserDao(new UserDaoOrcleImpl());
  11. // userService.getUser();
  12. //获取ApplicaitonContext:拿到Spring的容器
  13. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  14. //容器在手,天下我有,需要什么,g直接get什么!
  15. UserServiceImpl userService = (UserServiceImpl) context.getBean("UserServiceImpl");
  16. userService.getUser();
  17. }
  18. }

image.png