在学习Spring前,你需要明白反射到底是个什么东西?以及单例设计模式、工厂模式、代理模式,以及maven如何使用?
说明:这个Demo只是为了快速理解而已。
以下内容不做过多解释,对于以下内容不清楚的,可以看下我这篇文章
Spring入门-IoC&&DI
IDEA-2017
maven-3.6.0
springframework:5.0.2.RELEASE
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
package com;
public class Service {
private int age =22;
private String name = "张三";
public void Info(){
System.out.println("你要查询的是,"+name+"年龄为,"+age);
}
}
<?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">
<!--把对象的创建交给spring来管理-->
<bean id="service" class="com.Service"></bean>
</beans>
package com;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class User {
public static void main(String[] args) {
//1.获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象,取得Service类对象service
Service service=ac.getBean("service",Service.class);
service.Info();
}
}
//你要查询的是,张三年龄为,22
这种情况是用别人写的类,但是那个类没有默认构造函数而是通过其他方法构造的类。
package com.factory;
import com.Service;
//模拟一个工厂类(该类可能是存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
public class InstanceFactory {
public Service getService(){
return new Service();
}
}
此时bean.xml中应该这样写:
<bean id="instanceFactory" class="com.factory.InstanceFactory"></bean>
<bean id="service" factory-bean="instanceFactory" factory-method="getService"></bean>
package com.factory;
import com.Service;
public class StaticFactory {
public static Service getService(){
return new Service();
}
}
此时bean.xml中应该这样写:
<bean id="service" class="com.factory.StaticFactory" factory-method="getService"></bean>
此时先把Service类改造下:
package com;
public class Service {
private Integer age ;
private String name ;
public Service(Integer age,String name){
this.age=age;
this.name=name;
}
public void info(){
System.out.println("你要查询的是,"+name+"年龄为"+age);
}
}
此时bean.xml中应该这么写:
<bean id="service" class="com.Service">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
</bean>
在Service类中生成sett方法。
<bean id="service" class="com.Service">
<property name="name" value="李四"></property>
<property name="age" value="21"></property>
</bean>
导入context约束并告知spring在创建容器时要扫描的包
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com"></context:component-scan>
</beans>
此时的Service类:
package com;
import org.springframework.stereotype.Component;
@Component(value = "service")
public class Service {
public void info(){
System.out.println("你要查询的是,"+name+"年龄为"+age);
}
}
上面需要用到的User类都不做改变。
1)新建一个Dao类。
package com;
import org.springframework.stereotype.Component;
@Component("dao")
public class Dao {
public void Grade(){
System.out.println("他们都是大一的学生");
}
}
2)使用@Autowired自动按照类型注入。
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component(value = "service")
public class Service {
@Autowired
private Dao dao=null;
private Integer age ;
private String name ;
public void info(){
System.out.println("你要查询的是,"+name+"年龄为"+age);
}
public void Grade(){
dao.Grade();
}
}
在User类中加上 service.Grade();
方法
此时@Autowired的作用是去IoC容器中找数据类型为Dao的(即找到匹配的bean对象)。
其实就相当于在Service中新建了一个Dao类对象(在Service中注入Dao类对象)
@Value("18")
private Integer age ;
@Value("张三")
private String name ;