#题目1 1(14分)我们在示例中通过
context.getBean("header", StraightHeader.class);
方式来获取容器管理的对象,查找并学习Spring的API文档,列出getBean函数的其它形式接口,并解释各个接口的差异。
#回答 Spring Framework官方文档
Spring Framework API文档
根据API文档中ApplicationContext查看发现getBean方法是由BeanFactory接口进行声明的。也就是说最终继承了BeanFactory的这些对象都是获取Bean的方式。
根据题目列出getBean函数的其他形式接口,并解释各个接口的差异,解释为以哪些方式获取Bean对象,并如何使用。
##方式1:AnnotationConfigWebApplicationContext
ublic static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
另外一种形式
<web-app>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified @Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.AppConfig</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.acme.web.MvcConfig</param-value>
</init-param>
</servlet>
<!-- map all requests for /app/* to the dispatcher servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
</web-app>
##方式2:使用@Bean@Configuration @Import注解
@Configuration
public class ConfigA {
@Bean
public A a() {
return new A();
}
}
@Configuration
@Import(ConfigA.class)
public class ConfigB {
@Bean
public B b() {
return new B();
}
}
##方式3:ClassPathXmlApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext(
new String[] {"services.xml", "daos.xml"}, MessengerService.class);
##方式4:WebApplicationContext
@RunWith(SpringRunner.class)
// classpath resource
@WebAppConfiguration("classpath:test-web-resources")
// file system resource
@ContextConfiguration("file:src/main/webapp/WEB-INF/servlet-config.xml")
public class WacTests {
//
}
##方式5:ApplicationContextAware and BeanNameAware 实现ApplicationContextAware的方式重新编写获取getBean的逻辑
#题目2 2(16分) 假设我们有个应用(Application),它包含一个写文件的服务(FileWriterService),该服务在创建时会创建并打开一个文件,销毁时关闭该文件,并且该服务包含一个write(String content)方法:会将content写入到文件中,要求:
- 使用Spring IoC的相关内容组织该程序;
- 文件路径通过配置文件(properties)指定; (如果不清楚或者忘记如何打开,关闭以及写入文件,请回顾一个翁老师的 Java基础 课程)
基本要求:必须附加一个项目说明文档,说明每个功能点对应程序的运行结果(截图),项目的接口说明或者关键代码(不要把全部代码贴出来)等可以反映项目结果的内容。提交作业的时候必须有这个项目说明文档,否则会影响最终评分。
#回答 1.使用Spring IoC的相关内容组织该程序
回答1:这里使用Annotation的方式注入Bean对象
package com.hava.homework.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.*;
/**
* Created by zhanpeng on 2016/10/14.
*/
@Component
public class FileWriterService {
@Value("${file.path}")
String path;
FileOutputStream fileOutputStream;
@PostConstruct
public void init() throws FileNotFoundException {
System.out.println("Class FileWriterService Method init");
// open file
File file = new File(this.path);
if(file.exists())
{
}
else
{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
fileOutputStream = new FileOutputStream(file);
}
public void write(String content){
System.out.println("Class FileWriterService Method write Debug [content]:" + content);
//write content to the file
byte [] str_bytes = content.getBytes();
try {
fileOutputStream.write(str_bytes,0,str_bytes.length);
} catch (IOException e) {
e.printStackTrace();
}
}
@PreDestroy
public void destroy()
{
System.out.println("Class FileWriterService Method destroy");
// close file
if(fileOutputStream != null)
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.文件路径通过配置文件(properties)指定
回答2:文件的配置如下
#filename:file.properties
file.path=test.txt
##项目说明文档