小编主要介绍的是ioc中的两大重要作用:创建对象和DI(属性注入) Spring框架的底层原理? :xml解析技术,工厂模式,反射?
(小编就直接使用现成的图做一下描述)
对象的创建(基于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">
<!-- 配置User类的对象创建
id属性:创建对象的镜像别名
class:类的全路径
name:早期的镜像别名
-->
<bean id="user" class="com.java.sping5.User"></bean>
<!-- DI:注入文件的属性 通过xml配置文件-->
<bean id="Book" class="com.java.sping5.Book">
<!-- 属性注入 name:属性项 value:属性值-->
<property name="bname" value="张三是怎样装逼的"></property>
<property name="banthor" value="张三"></property>
<!--<property name="bname"><null></null></property> 设置属性是空值-->
</bean>
<!-- DI:注入文件的属性 通过有参数的构造-->
<bean id="oder" class="com.java.sping5.Oder">
<constructor-arg name="oname" value="redmi50"></constructor-arg>
<constructor-arg name="oadress" value="china"></constructor-arg>
</bean>
</beans>
?class类
package com.java.sping5;
//尝试用sping来创建对象并调用方法
public class User {
public void add(){
System.out.println("add a user");
}
}
package com.java.sping5;
public class Oder {
private String oname;
private String oadress;
public Oder(String oname, String oadress) {
this.oname = oname;
this.oadress = oadress;
}
@Override
public String toString() {
return "Oder{" +
"oname='" + oname + '\'' +
", oadress='" + oadress + '\'' +
'}';
}
public void output (){
System.out.println(oname);
System.out.println(oadress);
}
}
package com.java.sping5;
public class Book {
private String bname;
private String banthor;
public void setBname(String bname) {
this.bname = bname;
}
public void setBanthor(String banthor) {
this.banthor = banthor;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", banthor='" + banthor + '\'' +
'}';
}
}
属性注入
<?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-->
<bean id="userservice" class="com.java.sping5.service.UserService">
<!--注入UserDao对象-->
<property name="userDao" ref="userdao"></property>
</bean>
<bean id="userdao" class="com.java.sping5.dao.UserDaoimpl"></bean>
</beans>
<?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注入 级联赋值-->
<bean id="emp" class="com.java.sping5.Bean.Emp">
<!--基本属性-->
<property name="ename" value="张三"></property>
<property name="gender" value="男"></property>
<property name="dept">
<bean id="dept" class="com.java.sping5.Bean.Dept">
<property name="dname" value="安保部"></property>
</bean>
</property>
</bean>
</beans>
<?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="array" class="com.java.Bean.Dao.Array">
<!-- 数组注入-->
<property name="students">
<array>
<value>张三</value>
<value>李四</value>
<value>王伟</value>
</array>
</property>
<!--List集合-->
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王伟</value>
</list>
</property>
<!--Map集合-->
<property name="map">
<map>
<entry key="java" value="java入门到放弃"></entry>
<entry key="mysql" value="Mysql删库跑路"></entry>
</map>
</property>
<!--Set集合-->
<property name="set">
<set>
<value>Mysql</value>
<value>Rides</value>
</set>
</property>
<property name="courousList">
<!--集合中注入对象-->
<list>
<ref bean="courous1"></ref>
<ref bean="courous2"></ref>
</list>
</property>
</bean>
<bean id="courous1" class="com.java.Bean.Dao.Courous">
<property name="cname" value="java" ></property>
</bean>
<bean id="courous2" class="com.java.Bean.Dao.Courous">
<property name="cname" value="mysql" ></property>
</bean>
</beans>
对象创建基于(注解)
首先是在xml文件中开启文件扫描
<context:component-scan base-package="com.java.spring" >
</context:component-scan>
<!--表示的是扫描spring这个包-->
接口和实现类
package com.java.spring.Dao;
public interface UserDao {
public void add();
}
//实现类
package com.java.spring.Dao;
import org.springframework.stereotype.Repository;
@Repository(value = "userdaoimpl")
public class UserDaoimpl implements UserDao{
@Override
public void add() {
System.out.println("userdao add");
}
}
注解实现对象创建和测试
package com.java.spring;
import com.java.spring.Dao.UserDao;
import com.java.spring.Dao.UserDaoimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.EvaluationException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
//注解来创建对象 如果不添加(value="xxx") 默认是类的首字符小写名称
//@Component(value ="user") @Service
@Controller
public class User {
@Autowired // 根据类型进行注入 注解后不需set方法
@Qualifier(value = "userdaoimpl") //当一个接口的实现类较多的时候,就可以根据名称找的需要的类
private UserDao userDao;
@Value("张三")
private String uname;
public void add(){
System.out.println(uname);
userDao.add();
}
}
package com.java.spring;
import com.java.comfriger.Springcomfeiger;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Springtest {
@Test
public void test1(){
ApplicationContext context=
new ClassPathXmlApplicationContext("Bean4.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
@Test
public void test2(){
ApplicationContext context=
new AnnotationConfigApplicationContext(Springcomfeiger.class);
User user = context.getBean("user", User.class);
System.out.println(user);
user.add();
}
}
|