一、环境搭建
小皮面板:
小皮面板(phpstudy) - 让天下没有难配的服务器环境! (xp.cn)
sqlyog:
SQLyog - Download (softonic.com)
tomat:
源码、jar包:
链接:https://pan.baidu.com/s/1PlIHZXHIDH8zXp6CjARafQ
提取码:4567
--来自百度网盘超级会员V2的分享
二、简介
本篇演示set注入
标注: bean实例调用的是无参构造,如果需要bean对象的属性进行初始化,
就由容器来自动完成,就称为注入
流程:1.创建项目,导入jar包
2.定义类
3.创建Spring配置文件:编写bean
4.在测试类中测试
三、项目搭建,jar包导入
四、代码编写
pojo:
需要无参构造,tostring方法方便测试
School.java
package cn.lexed.pojo;
public class School {
private String name;
private String address;
public School() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "School [name=" + name + ", address=" + address + "]";
}
}
Student.java
package cn.lexed.pojo;
public class Student {
private String name;
private int age;
private School sc;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public School getSc() {
return sc;
}
public void setSc(School sc) {
this.sc = sc;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", sc=" + sc + "]";
}
}
service
Dog.java
package cn.lexed.service;
public interface Dog {
void eat();
}
SomeService.java
package cn.lexed.service;
public interface SomeService {
void some();
}
serviceImpl
Doglmpl
package cn.lexed.serviceImpl;
import cn.lexed.service.Dog;
public class DogImpl implements Dog{
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃ssss");
}
}
SomeService
package cn.lexed.serviceImpl;
import cn.lexed.service.SomeService;
public class SomeServiceImpl implements SomeService{
@Override
public void some() {
// TODO Auto-generated method stub
System.out.println("gsg");
}
}
resources:
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.xsd">
<!-- 告诉Spring,创建对象 -->
<!-- bean标签就是声明某个类的对象
id:对象的名称(自定义),唯一标识,通过id访问对象的
class:类的全限定名
SomeService s=new SomeServiceImpl();
spring框架,会把好的对象放到map集合里
springMap.put("id的值",new SomeServiceImpl())
springMap.put("s",new SomeServiceImpl())
-->
<bean id="s" class="cn.lexed.serviceImpl.SomeServiceImpl"></bean>
<bean id="ss" class="cn.lexed.serviceImpl.SomeServiceImpl"></bean>
<bean id="dog" class="cn.lexed.serviceImpl.DogImpl"></bean>
<bean id="d" class="java.util.Date"></bean>
<bean id="st" class="cn.lexed.pojo.Student"></bean>
<!-- set注入:设置值注入:spring调用类的set方法,完成属性赋值
property:name:属性名
value:属性值
复杂类的注入
property name:属性名
ref="bean"的id值
-->
<bean id="stu" class="cn.lexed.pojo.Student">
<property name="name" value="fjx"></property>
<property name="age" value="99"></property>
</bean>
<bean id="stus" class="cn.lexed.pojo.Student">
<!-- 简单类型 -->
<property name="name" value="大哥"></property>
<property name="age" value="99"></property>
<!-- 引用类型 -->
<property name="sc" ref="sch"></property>
</bean>
<bean id="sch" class="cn.lexed.pojo.School">
<property name="name" value="一中"></property>
<property name="address" value="南京"></property>
</bean>
</beans>
五、测试
package cn.lexed.test;
import static org.junit.Assert.*;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.lexed.pojo.Student;
import cn.lexed.serviceImpl.DogImpl;
import cn.lexed.serviceImpl.SomeServiceImpl;
public class TestSpring {
@Test
public void test() {
//1.创建spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
SomeServiceImpl ss=(SomeServiceImpl)ac.getBean("s");
ss.some();
}
@Test
public void test2() {
//1.创建spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
DogImpl dd=(DogImpl)ac.getBean("dog");
dd.eat();
Date date=(Date)ac.getBean("d");
System.out.println(date);
Student st=(Student)ac.getBean("stu");
System.out.println(st);
}
@Test
public void test3() {
//1.创建spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
Student st=(Student)ac.getBean("st");
System.out.println(st);
Student stu=(Student)ac.getBean("stu");
System.out.println(stu);
}
@Test
public void test4() {
//1.创建spring容器的对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//2.使用getBean方法,getBean(配置文件中bean的id值)
Student st=(Student)ac.getBean("stus");
System.out.println(st);
}
}
六、结果