Spring IOC

控制反转,通过一种方式(XML/注解),通过第三方去产生或者获取特定对象的方式。

《Java EE 亘联网轻量级框架整合开发-SSM 框架( Spring MVC+Spring+MyBatis )和 Redis 实现
》第九章实例解析-要橙汁,详细代码:https://download.csdn.net/download/xuwang777/10886388

1.  eclipse搭建spring:https://blog.csdn.net/peng86788/article/details/81166670

2.  juice.xml

<?--beans定义,根元素,引入xsd-->
<?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-4.0.xsd">
	 
        <?--依赖注入(setter方式)-->        
<?--定义bean编号id(自定义,首字母小写),class(类限定全名),property(定义类的属性),name(名称),value(值)-->
	<bean id="beanPostProcessor" class="bean.BeanPostProcessorImpl"/>
	<bean id="disPosableBean" class="bean.DisposableBeanImpl"/>
	
	<bean id="source" class="pojo.Source">
		<property name="fruit" value="橙"/>
		<property name="water" value="汁"/>
		<property name="size" value="小杯"/>
		<property name="sugar" value="少糖"/>
	</bean>	
	
	<bean id="juiceMaker" class="pojo.JuiceMaker"
		destroy-method="destroy" init-method="init">
		<property name="shop" value="爱心"/>
		<property name="source" ref="source"/>
	</bean>
</beans>

eclipse配置xml自动补全功能:https://blog.csdn.net/HH775313602/article/details/70176531

tips!!!!!!!!!!!!!!!!!!!!!!!!!!!!:   class="文件上方出现的package"

2. package bean;

重写了BeanPostProcessor以及DisposableBean的三个方法,其中BeanPostProcessor针对所有Bean,DisposableBean针对IOC容器本身。

3.  package pojo;'

source.java

        public String size;
	public String fruit;
	public String water;
	public String sugar;

自动补全set/get方法:source->generate steter/getter,快捷键:Ctrl+Shift+S

JuiceMaker.java

package pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class JuiceMaker implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean{
	private String shop=null;
	private Source source=null;
	
	public String getShop() {
		return shop;
	}
	public void setShop(String shop) {
		this.shop = shop;
	}
	public Source getSource() {
		return source;
	}
	public void setSource(Source source) {
		this.source = source;
	}
	
	public String maker() {
		String juice="这是"+shop+"饮料店的"+source.getSize()+source.getSugar()+source.getFruit()+source.getWater();
		return juice;	
	}
	
	private void init() {
		System.out.println("执行自定义初始化方法");
	}
	private void destroy() {
		System.out.println("执行自定义销毁方法");
	}
	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println(this.getClass().getSimpleName()+"调用afterPropertiesSet方法");
		
	}
	@Override
	public void setApplicationContext(ApplicationContext arg0) throws BeansException {
		System.out.println(this.getClass().getSimpleName()+"调用setApplicationContext方法");
		
	}
	@Override
	public void setBeanFactory(BeanFactory arg0) throws BeansException {
		System.out.println(this.getClass().getSimpleName()+"调用setBeanFactory方法");
		
	}
	@Override
	public void setBeanName(String arg0) {
		System.out.println(this.getClass().getSimpleName()+"调用setBeanName方法");
		
	}
	
	
	
}


自定义初始化方法和销毁方法,以及一些重写方法,执行顺序后面结合实验结果详述。

4.

package main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import pojo.JuiceMaker;

public class JuiceApplication {
	public static void main(String[] args) {
		testJuice();
	}
	public static void testJuice() {
		ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("juiceapplication.xml");		
		System.out.println("初始化IOC完成");

		JuiceMaker juiceMaker=(JuiceMaker) ctx.getBean("juiceMaker");
                System.out.println("获取Bean完成");

		System.out.println(juiceMaker.maker());		
		ctx.close();

		System.out.println("结束");
	}
		
}

5.实验结果

执行步骤 :

(1)初始化IOC容器.ClassPathXmlApplicationContex(".xml");

        load x beans

     creat shared instance (every bean id)

     初始化beans实例。

初始化中会执行.java中的方法,执行顺序如下图。

按照|.xml bean的加载顺序disposableBen->source->juiceMaker

开始实例化和实例化完成是执行BeanPostProcessor接口的PostProcessorBeforeInitialization方法和PostAfterInitialization方法,针对所有bean。

JuiceMaker.java中定义的方法的接口针对单个bean,所以只有juiceMaker实例会有那些执行语句,各种接口中的方法执行顺序如下图。

(2)获取Bean

		System.out.println("获取Bean开始");
		JuiceMaker juiceMaker=(JuiceMaker) ctx.getBean("juiceMaker");
		System.out.println("获取Bean完成");
		System.out.println(juiceMaker.maker());
		ctx.close();
		
		System.out.println("结束");

然后销毁,自定义销毁方法和针对IOC容器本身的接口DisposableBean的销毁方法。

6. 配置log4j.properties

log4j.rootLogger=INFO , stdout
log4j.logger.org.mybatis=INFO
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C: %m%n

 

全部评论

相关推荐

2025-12-15 14:25
云南大学 Java
lei22:入职可能会看学信网,最好别伪装,这个简历找实习肯定是够的,肯定会有收 28 届实习生的公司的,多投就行
点赞 评论 收藏
分享
已经入职字节快一个月了,突然想起来之前那段时间的面经没有发,先发一下timeline吧。Tiktok&nbsp;内容安全平台(人才库电话捞我):电话10.28&nbsp;-&gt;&nbsp;一面10.30(我觉得你跟我们组业务挺match的,然后过了三天问hr挂了,应该是别人流程更快)阿里淘天:投递11.11-&gt;约面11.12-&gt;一面11.14(问得很简单,30分钟,手撕八股全过无后续)Kpi面腾讯wxg&nbsp;微信小程序:投递11.13&nbsp;-&gt;约面11.14-&gt;&nbsp;一面11.17&nbsp;(究极无敌拷打,问我多模态大模型涉及的算法?但是人很好)-&gt;11.19流程终止小红书&nbsp;风控平台:投递11.16&nbsp;—约面11.17&nbsp;&nbsp;-&gt;一面11.18&nbsp;(抽象的面试官,面试感觉一般,问得前端网页安全相关的,确实没准备)-&gt;11.20挂百度&nbsp;百家号:投递11.14&nbsp;—&gt;约面11.14&nbsp;-&gt;一面11.14(当场约2面)-&gt;二面11.24-&gt;口头告知offer,&nbsp;拒绝(原因是业务不太好)美团&nbsp;技术平台投递11.17&nbsp;-&gt;&nbsp;约面(忘记了,没多久)&nbsp;-&gt;一面11.19&nbsp;-&gt;二面11.21&nbsp;(字节offer不想面了)快手&nbsp;电商业务投递11.17&nbsp;-&gt;&nbsp;约面11.18&nbsp;-&gt;一面11.19&nbsp;-&gt;&nbsp;二面11.21(拒了)腾讯wxg&nbsp;微信支付(被捞):(直接发面试邮件)技术一面12.05&nbsp;-&gt;技术二面12.11&nbsp;-&gt;技术三面12.17&nbsp;-&gt;&nbsp;hr面已拒绝(了解业务后拒绝,但是有转正hc,感觉还蛮可惜)字节跳动&nbsp;xxxx:东家就不放具体的时间线了,大概是面完第二天就能知道结果,除了有几天ld请假了没填面评。不去wxg还有个原因是还在期末周,深圳学校来回太麻烦了,至少现在在的组感觉能学到很多的东西,自己的选择应该也没错。还是感概一下,一年前大二的时候投简历海投基本上石沉大海,无论大小厂约面比例很少。现在基本上投了就有面试,还都是以前梦寐以求的大厂,现在自己也有了更多的选择,也没有投太多简历。也感谢上一段实习的经历,很有意思的项目,无论是字节,腾讯,还是美团基本都有聊这个项目。面经需要等一下,也许等周末有空了再发给各位uu,感兴趣可以关注一下~有想要交流学习的同学也可以私信我,目前人在北京大钟寺~,可以找搭子~
正能量的牛可乐:这么多大厂面试下来,不仅摸清了不同公司的面试风格,还能精准避雷业务不匹配的岗位,血赚
实习简历求拷打
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务