. What are the application scenarios, AOP principles, and benefits of AOP in spring?
A: Aop--aspect oriented programming is programming for facets and is used to encapsulate crosscutting concerns in the following scenarios:
Authentication permissions, Caching cache, context passing content delivery, error handling Errors handling lazy loading lazy loading, debugging debugging, logging, tracing, Profiling and monitoring record tracking optimization calibration, performance optimization performance optimization, persistence persistence, Resource pooling resource pool, synchronization synchronization , Transactions transactions
Principle: AOP is the aspect-oriented programming, which is to add unified function to the program through dynamic proxy, and to solve some public problems centrally.
Pros: 1. Good isolation coupling between individual steps is greatly reduced
2. Source code Independent, and then expand the functionality of the source and do not modify the operation
What is the role and principle of IOC in 2.Spring? The process of creating an object.
Answer: Ioc--inversion of control inversion. When a role needs to be assisted by another role, the caller's instance object is typically created by callers during a traditional program design process. But the work of creating the callee in spring is no longer done by the caller, so called control inversion. The work of creating the callee is done by spring, which is then injected directly into the caller's use.
3. Introduction to the Spring framework
It is a one-stop (full-stack full-stack) framework that provides a complete set of solutions from the presentation layer-SPRINGMVC to the business layer-spring to the persistence layer-springdata. We can only use the spring one framework in the project, it can provide the expression layer of the MVC framework, the persistence layer of the DAO framework. Its two core IOC and AOP provide support for our program decoupling and code simplicity and ease of maintenance.
4.Spring common annotations for creating objects?
Answer: @[email protected]@ [email protected] Repository
Design patterns used in 5.Spring
Answer: Simple factory, factory method, Singleton mode, adapter, wrapper, agent, observer, policy, template method
Detailed Description: Please refer to this Weibo: develop common design patterns
What are the advantages of 6.Spring?
A: 1. The coupling between components is reduced, and the decoupling between each layer of software is realized.
2. Can use many services that are easy to provide, such as transaction management, message service, etc.
3. The container provides a singleton mode support
4. The container provides AOP technology, it is easy to implement such as permission interception, run-time monitoring and other functions
5. The container provides many auxiliary classes, which can accelerate the development of the application
6.spring provides integrated support for mainstream application frameworks, such as Hibernate,jpa,struts
7.spring is a low-intrusive design with very low code pollution
8. Independent of various application servers
The 9.spring di mechanism reduces the complexity of business object substitution
10.Spring is highly open and does not force applications to rely entirely on spring, and developers are free to select part or all of spring
What is the difference between the scopes of 7.Spring beans?
Beans in a spring container can be divided into 5 ranges. The names of all scopes are self-explanatory, but to avoid confusion, let's explain:
Singleton: This bean range is the default, and this range ensures that no matter how many requests are received, there is only one instance of the bean in each container, and the singleton pattern is maintained by the Bean factory itself.
Prototype: The prototype range is the opposite of a singleton range, providing an instance for each bean request.
Request: an instance of each network request from the client is created within the scope of the requesting bean, and after the request is completed, the bean is invalidated and reclaimed by the garbage collector.
Session: Similar to the request scope, ensure that there is an instance of the bean in each session, and the bean will expire after the session expires.
Global-session:global-session is associated with portlet applications. When your app is deployed to work in a portlet container, it contains many portlets. If you want to declare that all portlets share a global storage variable, then this global variable needs to be stored in global-session.
The global scope has the same effect as the session scope in the servlet.
How many ways does 8.Spring manage transactions?
Answer: There are two ways:
1, programming transactions, hard coding in the code. (Not recommended)
2, declarative transactions, configured in the configuration file (recommended)
Declarative transactions are divided into two types:
A, XML-based declarative transactions
b, annotation-based declarative transactions
What are the automatic assembly methods in 9.spring?
Answer: 1, No: Automatic assembly is not enabled.
2, ByName: The name of the property to find the JavaBean-dependent object and inject it. For example, if the class computer has a property printer, specifying that its Autowire property is ByName, the Spring IOC container looks for a bean with Id/name property printer in the configuration file and injects it using the Seter method.
3. Bytype: Finds and injects JavaBean-dependent objects through the type of the property. For example, if the class computer has an attribute printer and the type is printer, the Spring IOC container will look for the bean with the class attribute bytype and inject it with the printer method after specifying its Autowire property as Seter.
4, constructor: Pass Bytype like, is also through the type to find dependent objects. The difference from Bytype is that it is not injected using the Seter method, but instead uses a constructor injection.
5. AutoDetect: Automatic choice of injection mode between Bytype and constructor.
6. Default: Determined by the Default-autowire attribute of the parent tag <beans>.
What are the core classes in 10.spring, and what are the roles?
Answer: beanfactory: Create a new instance, you can implement the singleton mode
Beanwrapper: Provides a unified get and set method
ApplicationContext: Provides framework implementations, including all features of the beanfactory
What are the 11.Bean invocation methods?
A: There are three ways to get a bean and make a call:
1. Using Beanwrapper
HelloWorld hw=new HelloWorld ();
Beanwrapper bw=new Beanwrapperimpl (HW);
Bw.setpropertyvalue ("msg", "HelloWorld");
System.out.println (Bw.getpropertycalue ("MSG"));
2. Using Beanfactory
InputStream is=new fileinputstream ("config");
Xmlbeanfactory factory=new xmlbeanfactory (IS);
HelloWorld hw= (HelloWorld) Factory.getbean ("HelloWorld");
System.out.println (Hw.getmsg ());
3. Using Applicationconttext
ApplicationContext actx=new flesystemxmlapplicationcontext ("config");
HelloWorld hw= (HelloWorld) Actx.getbean ("HelloWorld");
System.out.println (Hw.getmsg ());
12. What is the difference between what is IOC and what is di?
A: Dependency injection di is a programming pattern and architecture model, sometimes referred to as inversion of control, although, technically, dependency injection is a special implementation of IOC, dependency injection refers to an object that applies another object to provide a special ability, for example: putting a The database connection is passed as a parameter to the structure method of an object rather than creating a connection within that object itself. The basic idea of control inversion and dependency injection is to convert class dependencies from inside the class to the outside to reduce dependency
Applying control inversion, when an object is created, it is passed to it by an external entity that regulates all objects within the system, and references the object to which it depends. It can also be said that the dependency is injected into the object. So, control reversal is a reversal of this responsibility about how an object obtains a reference to the object he relies on.
There are two kinds of proxy methods for 13.spring:
A: If the target object implements several interfaces, spring uses the JDK's Java.lang.reflect.Proxy class proxy.
Advantage: The system is more loosely coupled because of the interface
Cons: Creating an interface for each target class
If the target object does not implement any interfaces, spring uses the Cglib library to generate a subclass of the target object.
Advantage: Because the proxy class is an inheritance relationship with the target class, there is no need for an interface to exist.
Disadvantage: Because the interface is not used, the coupling of the system does not use the dynamic proxy of the JDK well.
The 14.springMVC process?
Answer: 1. The user sends the request to the front controller Dispatcherservlet
2.DispatcherServlet received a request to call the Handlermapping processor mapper.
3. The processor mapper locates the specific processor according to the request URL, and the Processor object and processor interceptor (if any) are returned to Dispatcherservlet.
4.DispatcherServlet calling the processor via the Handleradapter processor adapter
5. Execute processor (Controller, also known as back-end controller).
6.Controller Execution Complete return Modelandview
7.HandlerAdapter returns the controller execution result Modelandview to Dispatcherservlet
8.DispatcherServlet passing Modelandview to Viewreslover view parser
9.ViewReslover parse to return to specific view
10.DispatcherServlet renders a view of the view (the model data is populated into the view).
11.DispatcherServlet Response User
Advantages of 15.SPRINGMVC
A: 1. It is based on component technology . All application objects , whether controllers and views, or business objects, are Java components . and tightly integrated with other infrastructure provided by spring.
2. Do not rely on the servlet API (although the target is, but it does depend on the servlet when it is implemented)
3. You can use any of the various view technologies, not just the JSP
4. Mapping strategies to support various request resources
5. It should be easy to expand
Also used struts2. What are the differences between SPRINGMVC and struts2 in brief?
The entrance to the SPRINGMVC is a servlet, the front-end controller, and the STRUTS2 entry is a filter filter.
Springmvc is based on method development (a URL corresponding to a method), request parameters passed to the parameter of the method, can be designed as a single case or multiple examples (recommended singleton), Struts2 is based on class development, passing parameters are through the properties of the class, can only be designed as multiple examples.
Struts uses value stacks to store request and response data, and access data through OGNL, SPRINGMVC through the parameter parser is to parse the request content, and assign values to the method parameters, the data and views are encapsulated into Modelandview objects, Finally, the model data in Modelandview is transferred to the page through the reques domain. The JSP view parser uses Jstl by default.
How is the 2.Spring transaction configured?
The transaction manager TransactionManager is configured first, and different frameworks have different properties.
Then configure the transaction notification and properties through Tx:advice.
Configure <aop:config>, set those methods or classes that need to join the transaction.
The 3.spring transaction control is placed in the service layer, and in the Service method one method invokes another method in the service, and several transactions are turned on by default.
Spring's transactional propagation method, by default, is propagation_required, determining whether a new transaction is currently open, a new transaction is added to the current transaction, or, if not, a new transaction is opened, so the answer is to open a transaction.
4.spring what happens when a transaction is rolled back?
The declarative transaction of Spring, EJB, by default, triggers a rollback of a transaction only after the unchecked exception is thrown
Unchecked exception, that is, run-time exception runntimeexception ROLLBACK TRANSACTION;
Checked exception, which is exception can try{}, is not rolled back. Of course, you can also configure the spring parameter to roll it back.
The transaction boundary of Spring begins before the business method is invoked, and execution of a commit or rollback is performed after the business method executes (spring by default depends on whether the runtime exception is thrown).
If the runtime exception is thrown and no catch is in your business method, the transaction is rolled back.
Generally do not need to catch the exception in the business method, if you want to catch, after the completion of the work you want to do (such as closing files, etc.) must be thrown runtime exception, or spring will be your operation commits, this will produce dirty data. So your catch code is superfluous.
What types of transaction management are supported by 5.Spring?
Spring supports two types of transaction management:
Programmatic transaction management: This means that you manage transactions programmatically, giving you great flexibility, but difficult to maintain.
Declarative transaction management: This means that you can separate business code from transaction management, and you only need to manage transactions with annotations and XML configurations.
What are the benefits of transaction management for 6.Spring frameworks?
It provides a constant programming pattern for different transactional APIs such as JTA,JDBC,HIBERNATE,JPA and JDO.
It provides a simple set of APIs for programmatic transaction management rather than some complex transactional APIs such as
It supports declarative transaction management.
It is well integrated with spring's various data access abstraction layers.
7. What about the seven major issues of spring?
In spring, the propagation properties of a transaction are set through propagation, and in this attribute we provide the characteristics of our transactional propagation:
Propagation_required: Supports the current transaction and creates a new transaction if there is no current transaction. This is the most common choice.
Propagation_supports: Supports the current transaction and is executed in a non-transactional manner if no transaction is currently in use.
Propagation_mandatory: Supports the current transaction and throws an exception if there is no current transaction.
Propagation_requires_new: Creates a new transaction and suspends the current transaction if a transaction is currently present.
Propagation_not_supported: Executes the operation in a non-transactional manner, suspending the current transaction if a transaction is currently present.
Propagation_never: Executes in a non-transactional manner and throws an exception if a transaction is currently present.
Propagation_nested: Supports current transaction, new SavePoint point, synchronous commit or rollback with current transaction.
8. What is the problem caused by transaction concurrency and how can I resolve it?
transaction concurrency can cause dirty reads, Phantom reads, non-repeatable reads, etc.Set up a transaction.Isolation Levelcan be solved.
9. Security of the transaction: how the lock mechanism is implemented and how it is used in the project?
Lock has pessimistic lock and optimistic lock, pessimistic lock generally assume that everyone will modify the data, by default, the data are locked, affecting performance, but high security.
Optimistic locking is the assumption that everyone reads only the data, does not modify the data, the performance is relatively high, but the security is low, generally by adding similar version control inside the version number to solve the problem.
10. Tell the difference between beanfactory and ApplicationContext?
Beanfactory is the spring container top-level core interface, relatively early, but less functionality, Getbean is beanfactory defined.
ApplicationContext is another container top-level interface in spring, it inherits from Beanfactory, but provides functions such as checksum, internationalization, monitoring, the bean management function is more, general use ApplicationContext .
11. A brief introduction to your understanding of MyBatis?
MyBatis Configuration
Sqlmapconfig.xml, this file is configured as a global configuration file for the MyBatis, with information such as the MyBatis runtime environment.
The Mapper.xml file is a SQL mapping file that is configured with SQL statements that manipulate the database. This file needs to be loaded in Sqlmapconfig.xml.
Construct Sqlsessionfactory as session factory through configuration information such as MyBatis environment
The sqlsession session is created by the session factory, and the operational database needs to be done through sqlsession.
MyBatis the bottom layer customizes the Executor actuator interface operation database, the executor interface has two implementations, one is the basic executor, the other is the cache executor.
Mapped statement is also mybatis a lower-level package object, which wraps mybatis configuration information and SQL mapping information. One SQL in the Mapper.xml file corresponds to a mapped statement object, and the SQL ID is the ID of the mapped statement.
Mapped statement defines SQL execution input parameters, including HashMap, base type, pojo,executor through Mapped statement to map the input Java objects into SQL before executing SQL. The input parameter mapping is the parameter set for PreparedStatement in JDBC programming.
Mapped statement defines SQL execution output, including HashMap, base type, pojo,executor through Mapped statement to map output results to Java objects after executing SQL. The output mapping process is equivalent to the parsing process of the results in JDBC programming.
12. Tell the difference between MyBatis and hibernate?
MyBatis is a lightweight package of JDBC that makes SQL and Java code independent, with relatively high performance, written SQL statements that are relatively flexible and easy to debug, and are typically used in large projects.
Hibernate is the heavyweight package of JDBC, the development speed is relatively fast, but the performance is relatively low, debugging is not convenient, generally suitable for the higher progress requirements of small and medium-sized projects.
What is the difference between #{} and ${} in 13.mybatis?
${} is a variable placeholder in the properties file, which can be used for tag attribute values and internal SQL, which is static text substitution, such as ${driver}, which is statically replaced with Com.mysql.jdbc.Driver.
#{} is a parameter placeholder for SQL, MyBatis will replace #{} in SQL with? Number, the PreparedStatement parameter setting method is used before SQL execution, and is ordered to SQL? The number placeholder sets the value of the parameter, such as Ps.setint (0, ParameterValue), #{item.name} to get the value of the Name property of the item object from the Parameter object using reflection, which is equivalent to Param.getitem (). GetName ().
What are the tags in the XML mapping file in 14.mybatis, in addition to the common Select|insert|updae|delete tags?
<resultMap>, <parameterMap>, <sql>, <include>, <selectkey>, plus 9 tags for dynamic sql, trim|where| Set|foreach|if|choose|when|otherwise|bind, where <sql> is the SQL Fragment label, the SQL fragment is introduced through the <include> tag,<selectkey> Generates a policy label for a primary key that does not support self-increment.
What is the 15.MyBatis programming step?
Create Sqlsessionfactory
Create sqlsession with Sqlsessionfactory
Performing database operations with sqlsession
Call Session.commit () to commit the transaction
Call Session.close () to close the session
What are the shortcomings of 16.JDBC programming, and how does mybatis solve these problems?
① Database link creation, release frequently results in system resource wasting, which can affect system performance, if the database link pool is used to resolve this problem.
Workaround: Configure the data link pool in Sqlmapconfig.xml and use connection pooling to manage database links.
②sql statements written in code cause code to be difficult to maintain, the actual application of SQL changes may be large, SQL changes need to change the Java code.
WORKAROUND: Configure the SQL statement to be detached from the Java code in the Xxxxmapper.xml file.
③ is troublesome to pass arguments to the SQL statement because the WHERE condition of the SQL statement is not necessarily, possibly more or less, and the placeholder needs to correspond to parameter one by one.
Resolution: MyBatis Automatically maps Java objects to SQL statements.
④ The result set parsing trouble, SQL changes lead to parsing code changes, and need to traverse before parsing, if the database records can be encapsulated into Pojo object resolution is more convenient.
Resolution: MyBatis automatically maps SQL execution results to Java objects.
17. What are the requirements when using the MyBatis Mapper interface call?
The Mapper interface method name is the same as the ID of each SQL defined in Mapper.xml
The input parameter type of the Mapper interface method is the same as the type of parametertype for each SQL defined in Mapper.xml
The output parameter type of the Mapper interface method is the same as the type of resulttype for each SQL defined in Mapper.xml
The namespace in the Mapper.xml file is the classpath of the Mapper interface.
18. Simply say MyBatis's first-level cache and level two cache?
MyBatis first go to the cache to query the result set, if not query the database, if there is a cache out of the returned result set will not go to the database. The MyBatis internal storage cache uses a Hashmap,keyhashcode+sqlid+sql Statements。 Value is the Java object generated from the query-out mapping
MyBatis's Level Two cache is the query cache, which is scoped to a mapper namespace, where querying SQL in the same namespace can fetch data from the cache. A secondary cache can be cross-sqlsession.
Does 19.Mybatis support lazy loading? If so, what is the principle of its implementation?
MyBatis only supports deferred loading of association associated objects and collection associated collection objects.Association refers to one-to-one , collection refers to a pair of multi-query。 In the MyBatis configuration file, you can configure whether lazy load Lazyloadingenabled=true|false is enabled.
The principle is to use Cglib to create a proxy object for the target object, and when the target method is called, enter the Interceptor method, such as Call A.GETB (). GetName (), The Interceptor Invoke () method discovers that A.getb () is a null value, then it will send the SQL of the pre-saved Query association B object separately, and the B query comes up. Then call A.SETB (b), so that the object B property of a has a value, and then completes the A.GETB (). The invocation of the GetName () method. This is the rationale behind lazy loading.
20.Mybatis XML mapping file, different XML mapping file, ID can be duplicated?
A different XML mapping file,if namespace is configured, the ID can be duplicated, and if no namespace is configured, then the ID cannot be duplicated;After all, namespace is not a must, just a best practice.
The reason is that namespace+id is used as map<string, mappedstatement> key, if there is no namespace, the ID is left, then the duplicate ID will cause the data to overwrite each other. With the namespace, the natural ID can be repeated, namespace different, Namespace+id nature is different.
21. Why is MyBatis a semi-automatic ORM mapping tool? What is the difference between it and the automatic?
Hibernate is a fully automated ORM Mapping tool that uses hibernatequerying associated ObjectsOr when associating a collection object, you can base theObject Relational ModelDirect access, so it is fully automatic. MyBatis, when querying an associated object or associated collection object, needs to write SQL manually, so called the semi-automatic ORM mapping tool.
22.SSM pros and cons, usage scenarios?
1. Unlike hibernate, MyBatis is not exactly an ORM framework, because MyBatis requires programmers to write their own SQL statements, but MyBatis can flexibly configure the SQL statements to run with XML or annotations. The Java object and the SQL statement map are generated to the final executed SQL, and the result of SQL execution is then mapped to the Java object.
2. MyBatis learning threshold is low, easy to learn, the programmer directly write the original ecological SQL, can strictly control the performance of SQL execution, flexibility, very suitable for the relational data model requirements of software development, Weifang UI training: such as Internet software, enterprise operations software, etc., because such software requirements change frequently, But demand changes require a rapid output. However, the premise of flexibility is that MyBatis can not do database independence, if you need to implement software that supports a variety of databases, you need to customize multiple sets of SQL mapping files, a lot of work.
3. Hibernate object/Relational mapping is strong, database independence is good, for the relational model of high-demand software (such as the need for fixed custom software) if Hibernate development can save a lot of code, improve efficiency. But hibernate's learning threshold is high, the threshold is higher, and how to design the O/R mapping, how to trade off between performance and object models, and how to use hibernate with great experience and ability.
4. In short, in accordance with the needs of users in a limited resource environment as long as the maintenance and extensibility of a good software architecture is a good architecture, so the framework only suitable is the best.
23.SPRINGMVC How is the interceptor implemented?
There are two ways, one is to implement an interface, and the other is toInheriting Adapter Classes, and then configure the interceptor in the SPRINGMVC configuration file:
What are the application scenarios, AOP principles, and benefits of AOP in 24.Spring?
Aop--aspect oriented programming is programming for facets and is used to encapsulate crosscutting concerns in the following scenarios:
Authentication permissions, Caching cache, context passing content delivery, error handling Errors handling lazy loading lazy loading, debugging debugging, logging, tracing, Profiling and monitoring record tracking optimization calibration, performance optimization performance optimization, persistence persistence, Resource pooling resource pool, synchronization synchronization , Transactions transactions
Principle: AOP is the aspect-oriented programming, which is to add unified function to the program through dynamic proxy, and to solve some public problems centrally.
Advantages:
1. Good isolation coupling between each step is greatly reduced
2. Source code Independent, and then expand the functionality of the source and do not modify the operation.
Spring Mabatis Springmvc has seen