标签:type 依赖 http struct ade 应用 轻量级 上下文 ase
轻量级(框架api与代码耦合很低)的java开发框架
spring是开源的设计层面的框架,它解决的是业务逻辑层和其他各层的松耦合问题,它将面向接口的编程思想遍布整个应用。
spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.0.RELEASE</version>
</dependency>
public class User {
private List<String> list;
private Map<String,String> map;
private String name;
}
<?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 class="com.hello.bean.User" id="user">
<property name="name" value="121"/>
<property name="list">
<list>
<value>fdf</value>
<value>fdf</value>
</list>
</property>
<property name="map">
<map>
<entry key="age" value="18"></entry>
<entry key="car" value="11"></entry>
</map>
</property>
</bean>
</beans>
@Test
public void test1(){
//获取上下文对象
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("application.xml");
//从上下文对象中获取类对象(根据id或者类的字节码文件)
User user = context.getBean("user", User.class);
System.out.println(user);
}
注:ClassPathXmlApplicationContext可加载多个配置文件
1、传入String数组
2、在加载的xml文件中使用
spring的配置文件中可使用三种方法为对象属性赋值
<bean class="com.hello.bean.User" id="user"/>
<bean class="com.hello.bean.Admin" id="admin">
<constructor-arg name="name" value="zhang"/>
<constructor-arg name="pwd" value="AAd"/>
<constructor-arg name="user" ref="user"/>
</bean>
constructor-arg 标签对应构造器注入,name是属性名,value是属性值,ref是引用数据类型使用
注:
?
根据类对象的属性类型赋值
类中必须要有setter方法
<bean class="com.hello.bean.User" id="user"/>
<bean class="com.hello.bean.Admin" id="admin">
<property name="name" value="asd"/>
<property name="pwd" value="23"/>
<property name="user" ref="user"/>
</bean>
3、接口注入
不推荐
<bean class="com.hello.bean.User" id="user">
<property name="name" value="121"/>
<property name="list">
<list>
<value>fdf</value>
<value>fdf</value>
</list>
</property>
<property name="map">
<map>
<entry key="age" value="18"></entry>
<entry key="car" value="11"></entry>
</map>
</property>
</bean>
<util:list id="list">
<value>ada</value>
<value>ada</value>
</util:list>
<bean class="com.hello.bean.User" id="user"/>
<bean class="com.hello.bean.Admin" p:name="asd" p:pwd="add" p:user-ref="user"/>
<bean class="com.hello.bean.Teacher" id="teacher">
<property name="user" ref="user"/>
<property name="tname" value="#{user.name}"/>
</bean>
即直接new对象
<bean class="com.hello.bean.User" id="user"/>
<bean class="com.hello.bean.Admin" id="admin">
<property name="name" value="asd"/>
<property name="pwd" value="23"/>
<property name="user" ref="user"/>
</bean>
类:
public class People {
String name;
}
对象工厂:
public class BeanFactory {
public static People getPeople(){
return new People();
}
}
spring获取方式:
<bean id="people" class="com.hello.factory.BeanFactory" factory-method="getPeople">
<property name="name" value="434"/>
</bean>
类:
public class People {
String name;
}
实例工厂:
public class FactoryBean {
public People getPeople(){
return new People();
}
}
从实例化工厂获取对象:
<!--实例化工厂-->
<bean class="com.hello.factory.FactoryBean" id="factoryBean"/>
<!--从工厂获取对象-->
<bean class="com.hello.bean.People" factory-bean="factoryBean" factory-method="getPeople">
<property name="name" value="w3"/>
</bean>
标签:type 依赖 http struct ade 应用 轻量级 上下文 ase
原文地址:https://www.cnblogs.com/ygfcoder/p/13384206.html