Spring--Spring入门
2021-07-13 13:06
标签:1.0 工厂 编程 spring ase group get his 项目 UserService UserServiceImpl SpringDemo1 applicationContext.xml
Spring--Spring入门 标签:1.0 工厂 编程 spring ase group get his 项目 原文地址:https://www.cnblogs.com/windbag7/p/9542300.htmlSpring的概述-什么是Spring
Spring的概述-什么是Spring
Spring的概述-Spring的优点
Spring的概述-Spring的模块
Spring的Ioc的底层实现
Spring的Ioc的入门案例
dependencies>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>4.11version>
scope>testscope>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-coreartifactId>
version>4.3.7.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>4.3.7.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-beansartifactId>
version>4.3.7.RELEASEversion>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-expressionartifactId>
version>4.3.7.RELEASEversion>
dependency>
dependency>
groupId>commons-logginggroupId>
artifactId>commons-loggingartifactId>
version>1.2version>
dependency>
dependency>
groupId>log4jgroupId>
artifactId>log4jartifactId>
version>1.2.17version>
dependency>
dependencies>
public interface UserService {
public void sayHello();
}
public class UserServiceImpl implements UserService{
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//添加属性
private String name;
@Override
public void sayHello() {
System.out.print("hello spring" + name);
}
}
public class SpringDemo1 {
@Test
/**
* 传统方式开发
*/
public void demo1(){
//UserService userService = new UserServiceImpl();
UserServiceImpl userService = new UserServiceImpl();
//设置属性 传统方法要改代码 就不好了
userService.setName("张三");
userService.sayHello();
}
@Test
/**
* 使用Spring 的方式
*/
public void demo2(){
//创建Spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类
UserService userService = (UserService) applicationContext.getBean("userService");
userService.sayHello();
}
}
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.xsd">
bean id="userService" class="com.windbag.ioc.demo1.UserServiceImpl">
property name="name" value="李四">property>
bean>
beans>
Spring IOC的快速入门案例