spring interview questions

1. What are the benefits of using the Spring framework?

  • Lightweight: Spring is lightweight.
  • Inversion of control: The process of manually new objects and the process of managing objects are handed over to Spring to manage. Implement inversion of control.
  • Aspect-Oriented Programming (AOP): Spring supports Aspect-Oriented Programming and separates application business logic from system services.
  • Container: Spring contains and manages the lifecycle and configuration of objects in the application.
  • MVC framework : The WEB MVC framework is used, which provides developers with other frameworks besides Struts and data persistence frameworks. And the use of springmvc can be well combined with spring, which will reduce many compatibility problems compared to the integration of spring and other frameworks. Do the project quickly.
  • Transaction management: Spring provides a persistent transaction management interface that can be extended from local transactions down to global transactions (JTA).
  • Exception handling: Spring provides a convenient API to convert technology-specific exceptions (such as those thrown by JDBC, Hibernate or JDO) into consistent unchecked exceptions.

2. Use spring to manage bena

 The spring core container is a super factory. All objects will be treated as objects managed by the spring core container. Spring calls all objects in the container beans. Beans in spring are different from java beans. Java beans need to follow some specific specifications, there is no requirement in spring', as long as it is a java class, spring can manage the java class. and treat it as a bean. That is, all objects are beans

example:

public class Axe {
   public String chop(){
	   return "Call Axe method";
   }
}
//Inject via setter method
public class Person{
	private Axe axe;
	public void setAxe(Axe axe){
		this.axe=axe;
	}
	public void useAxe(){
		System.out.println(axe.chop);
	}
}
//Inject via constructor
public class Person{
	private Axe axe;
	public Person(Axe axe){
		this.axe=axe;
	}
	public void useAxe(){
		System.out.println(axe.chop);
	}
}
configuration file
<beans>
<bean id ="person" classs="Person">
    <property name="axe" ref="axe">
</bean>
<bean id ="axe" classs="Axe"/>
<beans/>

 

3. The core mechanism of spring: dependency injection

Dependency: The situation in which object A needs to call the method of object B, in this case Spring is called a dependency.

Two core functions of the spring framework:

First: spring is responsible for creating and managing all java objects, these java objects are called beans

Second: the spring container manages the dependencies between beans. Spring uses dependency injection to manage dependencies between beans. (Dependency injection and inversion of control mean the same thing)

After using the spring framework, the caller does not need to actively obtain the dependent object, but passively accepts the spring container to assign values ​​to the caller's member variables.

The way of dependency injection:

Setter injection: The IOC container uses the setter method of member variables to inject dependent objects

Constructor injection: The IOC container uses a constructor to inject dependent objects

4. Use of spring container

The spring container has two core interfaces, BeanFactory and ApplicationContext. ApplicationContext is a sub-interface of BeanFactory. Spring configuration files usually need to be passed in using a Resource object. Most of them will not use BeanFactory instance as the spring container, but use ApplicationContext as the container, so the spring container is also the spring context. , and enhance the function of BeanFactory.

Since spring is interface-oriented programming, both the caller and the dependent object should be defined as interfaces, and programs should face their interfaces, not programming implementation classes. Easy to upgrade and maintain the above code rewriting

public interface Axe {
   public String chop();
}
public class StoneAxe implements Axe{
	public String chop(){
		   return "Call Axe method";
	   }
}

public interface Person{
	public void useAxe();
}
public class Chinese implements Person{
	private Axe axe;
	public Chinese(Axe axe){
		this.axe=axe;
	}
	public void useAxe(){
		System.out.println(axe.chop);
	}
}
<beans>
<bean id ="chinese" class="Chinese">
    <constructor-arg ref="stoneAxe"/>
</bean>
<bean id ="stoneAxe" class="StoneAxe">
<beans/>
//pass values ​​to methods in a class
<bean id="" class="">
   <property name="test" value="wukong">
</bean> Note: This configuration is a parameter set to the setTest method in the class

 

4. What is the difference between BeanFactory and ApplicationContext?

 BeanFactory can be understood as a factory class containing a collection of beans. BeanFactory contains definitions of various beans, so that when a client request is received, the corresponding

bean instantiation.


BeanFactory can also generate relationships between collaborating classes when instantiating objects. This frees up the configuration of the bean itself and the bean client.

BeanFactory also includes the control of the bean life cycle, calling client initialization methods (initialization methods) and destruction methods (destruction methods).


On the surface, the application context, like the bean factory, has bean definitions, bean association settings, and the function of distributing beans according to requests.

But application context also provides other functions on this basis.


1. Provides text messages that support internationalization

2. Unified resource file reading method

3. Events of beans that have been registered in the listener


The following are three of the more common ApplicationContext implementations:

1. ClassPathXmlApplicationContext: Read the context from the XML configuration file of the classpath and generate the context definition. The application context is obtained from the program environment variable.

ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

2. FileSystemXmlApplicationContext: The context is read from the XML configuration file in the file system.

ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");

3. XmlWebApplicationContext: The context is read from the XML file of the Web application.

 

 

5. How many configuration methods does Spring have?

There are three ways to configure Spring into application development:

1、基于XML的配置

2、基于注解的配置

3、基于Java的配置

 

7、如何用基于XML配置的方式配置Spring?

在Spring框架中,依赖和服务需要在专门的配置文件来实现,我常用的XML格式的配置文件。

这些配置文件的格式通常用开头,然后一系列的bean定义和专门的应用配置选项组成。


SpringXML配置的主要目的时候是使所有的Spring组件都可以用xml文件的形式来进行配置。

这意味着不会出现其他的Spring配置类型(比如声明的方式或基于Java Class的配置方式)


Spring的XML配置方式是使用被Spring命名空间的所支持的一系列的XML标签来实现的。

Spring有以下主要的命名空间:context、beans、jdbc、tx、aop、mvc和aso。

<beans>
    <!-- JSON Support -->
    <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
    <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>
</beans>

下面这个web.xml仅仅配置了DispatcherServlet,这件最简单的配置便能满足应用程序配置运行时组件的需求。

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
        <servlet-name>spring</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

 

8、如何用基于Java配置的方式配置Spring?

Spring对Java配置的支持是由@Configuration注解和@Bean注解来实现的。由@Bean注解的方法将会实例化、

配置和初始化一个新对象,这个对象将由Spring的IoC容器来管理。@Bean声明所起到的作用与 元素类似。

被@Configuration所注解的类则表示这个类的主要目的是作为bean定义的资源。被@Configuration声明的类可以

通过在同一个类的内部调用@bean方法来设置嵌入bean的依赖关系。


最简单的@Configuration 声明类请参考下面的代码:

@Configuration
public class AppConfig{
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

对于上面的@Beans配置文件相同的XML配置文件如下:

<beans>
    <bean id="myService" class="com.howtodoinjava.services.MyServiceImpl"/>
</beans>

上述配置方式的实例化方式如下:利用AnnotationConfigApplicationContext 类进行实例化

public static void main(String[] args) {
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
    MyService myService = ctx.getBean(MyService.class);
    myService.doStuff();
}

要使用组件组建扫描,仅需用@Configuration进行注解即可:

@Configuration
@ComponentScan(basePackages = "com.howtodoinjava")
public class AppConfig  {
    ...
}

在上面的例子中,com.acme包首先会被扫到,然后再容器内查找被@Component 声明的类,

找到后将这些类按照Sring bean定义进行注册。


如果你要在你的web应用开发中选用上述的配置的方式的话,需要用AnnotationConfigWebApplicationContext类来读取配置文件,

可以用来配置Spring的Servlet监听器ContrextLoaderListener或者Spring MVC的DispatcherServlet。

<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.howtodoinjava.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.howtodoinjava.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>

 

 

9、怎样用注解的方式配置Spring?

Spring在2.5版本以后开始支持用注解的方式来配置依赖注入。可以用注解的方式来替代XML方式的bean描述,

可以将bean描述转移到组件类的内部,只需要在相关类上、方法上或者字段声明上使用注解即可。

注解注入将会被容器在XML注入之前被处理,所以后者会覆盖掉前者对于同一个属性的处理结果。


注解装配在Spring中是默认关闭的。所以需要在Spring文件中配置一下才能使用基于注解的装配模式。

如果你想要在你的应用程序中使用关于注解的方法的话,请参考如下的配置。

<beans>

   <context:annotation-config/>
   <!-- bean definitions go here -->

</beans>

context:annotation-config/标签配置完成以后,就可以用注解的方式在Spring中向属性、方法和构造方法中自动装配变量。


下面是几种比较重要的注解类型: 

1、@Required:该注解应用于设值方法。

2、@Autowired:该注解应用于有值设值方法、非设值方法、构造方法和变量。

3、@Qualifier:该注解和@Autowired注解搭配使用,用于消除特定bean自动装配的歧义。

4、JSR-250 Annotations:Spring支持基于JSR-250注解的以下注解,@Resource、@PostConstruct和@PreDestroy。

 

 

11、Spring Bean的作用域之间有什么区别?

Spring容器中的bean可以分为5个范围。所有范围的名称都是自说明的,但是为了避免混淆,还是让我们来解释一下:

1、singleton:每次请求该bean都将获得相同的实例容器负责跟踪bean实例的状态维护bean的生命周期。

2、prototype:程序每次请求该id的bean,spring都会新建一个bean实例,然后返回给程序。这种情况使用new关键字创建bean实例,一旦创建成功,容器不在跟踪实例

3、request:在请求bean范围内会每一个来自客户端的网络请求创建一个实例,在请求完成以后,bean会失效并被垃圾回收器回收.默认singleton

4、Session:与请求范围类似,确保每个session中有一个bean的实例,在session过期后,bean会随之失效。

5、global-session:global-session和Portlet应用相关。当你的应用部署在Portlet容器中工作时,它包含很多portlet。

如果你想要声明让所有的portlet共用全局的存储变量的话,那么这全局变量需要存储在global-session中。


全局作用域与Servlet中的session作用域效果相同。

 

Spring注解

36. 什么是基于Java的Spring注解配置? 给一些注解的例子.

基于Java的配置,允许你在少量的Java注解的帮助下,进行你的大部分Spring配置而非通过XML文件。

以@Configuration 注解为例,它用来标记类可以当做一个bean的定义,被Spring IOC容器使用。另一个例子是@Bean注解,它表示此方法将要返回一个对象,作为一个bean注册进Spring应用上下文。

37. 什么是基于注解的容器配置?

相对于XML文件,注解型的配置依赖于通过字节码元数据装配组件,而非尖括号的声明。

开发者通过在相应的类,方法或属性上使用注解的方式,直接组件类中进行配置,而不是使用xml表述bean的装配关系。

38. 怎样开启注解装配?

注解装配在默认情况下是不开启的,为了使用注解装配,我们必须在Spring配置文件中配置 <context:annotation-config/>元素。

39. @Required  注解

这个注解表明bean的属性必须在配置的时候设置,通过一个bean定义的显式的属性值或通过自动装配,若@Required注解的bean属性未被设置,容器将抛出BeanInitializationException。

40. @Autowired 注解

@Autowired 注解提供了更细粒度的控制,包括在何处以及如何完成自动装配。它的用法和@Required一样,修饰setter方法、构造器、属性或者具有任意名称和/或多个参数的PN方法。

41. @Qualifier 注解

当有多个相同类型的bean却只有一个需要自动装配时,将@Qualifier 注解和@Autowire 注解结合使用以消除这种混淆,指定需要装配的确切的bean。

 

 

Spring支持的事务管理类型

Spring支持两种类型的事务管理:

  • 编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵活性,但是难维护。
  • 声明式事务管理:这意味着你可以将业务代码和事务管理分离,你只需用注解和XML配置来管理事务。

 

Spring面向切面编程(AOP)

51.  解释AOP

面向切面的编程,或AOP, 是一种编程技术,允许程序模块化横向切割关注点,或横切典型的责任划分,如日志和事务管理。

52. Aspect 切面

AOP核心就是切面,它将多个类的通用行为封装成可重用的模块,该模块含有一组API提供横切功能。比如,一个日志模块可以被称作日志的AOP切面。根据需求的不同,一个应用程序可以有若干切面。在Spring AOP中,切面通过带有@Aspect注解的类实现。

52. 在Spring AOP 中,关注点和横切关注的区别是什么?

关注点是应用中一个模块的行为,一个关注点可能会被定义成一个我们想实现的一个功能。
横切关注点是一个关注点,此关注点是整个应用都会使用的功能,并影响整个应用,比如日志,安全和数据传输,几乎应用的每个模块都需要的功能。因此这些都属于横切关注点。

54. 连接点

连接点代表一个应用程序的某个位置,在这个位置我们可以插入一个AOP切面,它实际上是个应用程序执行Spring AOP的位置。

55. 通知

通知是个在方法执行前或执行后要做的动作,实际上是程序执行时要通过SpringAOP框架触发的代码段。

Spring切面可以应用五种类型的通知:

  • before:前置通知,在一个方法执行前被调用。
  • after: 在方法执行之后调用的通知,无论方法执行是否成功。
  • after-returning: 仅当方法成功完成后执行的通知。
  • after-throwing: 在方法抛出异常退出时执行的通知。
  • around: 在方法执行之前和之后调用的通知。

56. 切点

切入点是一个或一组连接点,通知将在这些位置执行。可以通过表达式或匹配的方式指明切入点。

57. 什么是引入? 

引入允许我们在已存在的类中增加新的方法和属性。

58. 什么是目标对象? 

被一个或者多个切面所通知的对象。它通常是一个代理对象。也指被通知(advised)对象。

59. 什么是代理?

代理是通知目标对象后创建的对象。从客户端的角度看,代理对象和目标对象是一样的。

60. 有几种不同类型的自动代理?

BeanNameAutoProxyCreator

DefaultAdvisorAutoProxyCreator

Metadata autoproxying

61. 什么是织入。什么是织入应用的不同点?

织入是将切面和到其他应用类型或对象连接或创建一个被通知对象的过程。

织入可以在编译时,加载时,或运行时完成。

62. 解释基于XML Schema方式的切面实现。

在这种情况下,切面由常规类以及基于XML的配置实现。

63. 解释基于注解的切面实现

在这种情况下(基于@AspectJ的实现),涉及到的切面声明的风格与带有java5标注的普通java类一致。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326402111&siteId=291194637