spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用
把创建对象和维护对象之间的关系权利转交给spring管理,spring容器控制对象的创建,注入需要注入的对象
通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术
隔离业务逻辑,降低耦合度,提高可用性和开发效率,主要用于日志记录,事务管理,异常处理等等
3.0版本后,根据需要引入模块需要的包,进行模块开发
创建对象
@data public class person { 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 id="person" class="top.mjsmry.demo01.person" name="person"> </bean> </beans>
创建测试方法
public class springdemotest { @test public void tes01() { //实例化管理对象 classpathresource resource = new classpathresource("/top/mjsmry/demo01/spring-config.xml"); xmlbeanfactory factory = new xmlbeanfactory(resource); //1根据id创建 person u = (person) factory.getbean("person"); u.setname("lili"); system.out.println(u); } }
//2根据name和class person person = factory.getbean("person2", person.class); person.setname("lili"); //3直接根据字节码 但是配置文件必须唯一,一个对象配置多个bean的话,spring会无法选择 person bean = factory.getbean(person.class); system.out.println(person);
单独节点: lazy-init="true"
spring测试可以在不启动spring项目情况下进行单元测试
@contextconfiguration("spring-config.xml") @runwith(springjunit4classrunner.class) public class springtest { @autowired person person; @test public void test() { person.setname("lili"); system.out.println(person); } }
<bean id="person" class="top.mjsmry.singletonandprototype.person" scope="prototype"/>
scope prototype多例 singleton单例
public class springtest { @test public void test() { applicationcontext ac=new classpathxmlapplicationcontext("top/mjsmry/singletonandprototype/spring-config.xml"); person person1= (person) ac.getbean("person"); person person2= (person) ac.getbean("person"); //单例的话就是同一个对象 system.out.println(person1==person2);//true } }
子bean可以覆盖父bean
<!-- 模板bean --> <bean id="p" class="top.mjsmry.beanandbean.person" abstract="true"> <property name="name" value="张三"></property> </bean> <bean id="person" class="top.mjsmry.beanandbean.person" parent="p"/> <bean id="person2" class="top.mjsmry.beanandbean.person" parent="p"> <property name="name" value="子覆盖了"></property> </bean>
@test public void test() { applicationcontext ac=new classpathxmlapplicationcontext("top/mjsmry/beanandbean/spring-config.xml"); person person= (person) ac.getbean("person"); system.out.println(person); person person2= (person) ac.getbean("person2"); system.out.println(person2); }
<!-- 生命周期 构造方法 getset init-method 初始化 destroy-method 销毁 --> <bean id="person" class="top.mjsmry.beancycle.person" init-method="init" destroy-method="destory"/>
bean
@data public class person { private string name; public person() { system.out.println("构造方法调用了"); } private void init() { system.out.println("--init--"); } private void destory() { system.out.println("--destory--"); } }
<!-- 实现beanpostprocessor 细致的声明周期 postprocessbeforeinitialization 初始化方法之前 postprocessafterinitialization 初始化方法之后 --> <bean id="personbeanpostprocessor" class="top.mjsmry.beancycle.personbeanpostprocessor"/>
实现接口
public class personbeanpostprocessor implements beanpostprocessor { @override public object postprocessbeforeinitialization(object o, string s) throws beansexception { system.out.println("postprocessbeforeinitialization"); return o; } @override public object postprocessafterinitialization(object o, string s) throws beansexception { system.out.println("postprocessafterinitialization"); return o; } }
测试
@test public void test() { classpathxmlapplicationcontext ca=new classpathxmlapplicationcontext("top/mjsmry/beancycle/spring-config.xml"); person person= (person) ca.getbean("person"); ca.close(); }
dao
public class testdao { public int add() { return 1; } }
service
public interface testservice { string add(); }
@data public class testserviceimpl implements testservice { private testdao testdao; @override public string add() { return testdao.add()==1?"添加成功":"添加失败"; } }
controller
@data public class testcontroller { private testservice testservice; public string add() { return testservice.add(); } }
配置文件
<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"> <!-- set注入 --> <bean id="testcontroller" class="top.mjsmry._01.controller.testcontroller"> <property name="testservice" ref="testservice"/> </bean> <bean id="testservice" class="top.mjsmry._01.service.impl.testserviceimpl"> <property name="testdao" ref="testdao"/> </bean> <bean id="testdao" class="top.mjsmry._01.dao.testdao"></bean> </beans>
test
@runwith(springjunit4classrunner.class) @contextconfiguration("/spring-config.xml") public class mytest { @autowired private testcontroller testcontroller; @test public void test01() { string result = testcontroller.add(); system.out.println(result); } }
测试结果
<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-4.2.xsd "> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置一个dbcp的bean --> <bean name="datesource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <!-- 注意:这里我们不是使用的ref引用,而是直接写的value,因此注入的数据是一个变通的值 --> <property name="driverclassname" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> </beans>
db.properties
jdbc.driver=com.mysql.cj.jdbc.driver jdbc.url=jdbc:mysql://localhost:3306/jdbcwork?usessl=false&servertimezone=utc jdbc.username=xxx jdbc.password=xxx
测试
@runwith(springjunit4classrunner.class) @contextconfiguration("spring-config.xml") public class datasourcetest { @autowired basicdatasource basicdatasource; @test public void test01() { try { connection connection = basicdatasource.getconnection(); connection.close(); } catch (sqlexception e) { e.printstacktrace(); } } }
无报错测试通过
@data @allargsconstructor @noargsconstructor public class person { private string username; private car car; private string[] strings; private list<string> list; private set<string> set; private list<wife> wifelist; private properties p1; private properties p2; }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 外部bean注入 --> <bean id="car" class="top.mjsmry._05other.car"> <property name="price" value="1.0"/> <property name="type" value="bwm"/> </bean> <bean id="person" class="top.mjsmry._05other.person"> <property name="username" value="张三"/> <property name="car" ref="car"/> </bean> <!-- 内部bean定义 --> <bean id="person2" class="top.mjsmry._05other.person"> <property name="username" value="张三"/> <property name="car"> <bean class="top.mjsmry._05other.car"> <property name="price" value="1.0"/> <property name="type" value="bwm"/> </bean> </property> </bean> <!-- 其他类型的注入 --> <bean id="person3" class="top.mjsmry._05other.person"> <property name="username" value="张三"/> <property name="car"> <bean class="top.mjsmry._05other.car"> <property name="price" value="1.0"/> <property name="type" value="bwm"/> </bean> </property> <!-- 数组 --> <property name="strings" value="lili,keke"/> <!-- 集合 --> <property name="list"> <list> <value>1</value> <value>2</value> <value>3</value> </list> </property> <!-- set --> <property name="set"> <set> <value>k</value> <value>e</value> <value>w</value> </set> </property> <!-- 泛型 --> <property name="wifelist"> <list> <bean class="top.mjsmry._05other.wife"> <property name="username" value="lili"/> </bean> </list> </property> <!-- properties注入--> <property name="p1"> <value>prokey1=provalue1</value> </property> <property name="p2"> <props> <prop key="键1">值1</prop> </props> </property> </bean> </beans>
测试
public class othertest { @test public void test01() { applicationcontext ac=new classpathxmlapplicationcontext("/top/mjsmry/_05other/spring-config.xml"); person person= (person) ac.getbean("person"); person person2= (person) ac.getbean("person2"); person person3= (person) ac.getbean("person3"); system.out.println(person); system.out.println(person2); system.out.println(person3); } }
关注
更多内容关注
如对本文有疑问, 点击进行留言回复!!
NullPointerException: Attempt to invoke virtual method ‘android.content.res.XmlResourceParser androi
关于启动appium-desktop,报错:Cannot extract apk info using apkanalyzer. Falling back to aapt. Original ....
Gradle 发布共享库——如何通过Gradle发布Android依赖库(aar)到 jitpack 公共仓库
Gradle 发布共享库——如何通过Gradle发布java依赖库(jar)到 jitpack 公共仓库(—)
网友评论