10 basic java questions (second bullet)

11. What is reflection and what can reflection do?

Reflection: Simply put, the reflection mechanism is worthy of the program being able to obtain its own information at runtime. In java, as long as the name of the class is given, all the information of the class can be obtained through the reflection mechanism. The function of reflection is actually: at runtime, it is possible to determine the class to which any object belongs, and to construct an object of any class at runtime. Our commonly used Spring framework also uses the Java reflection architecture and also at runtime To determine the member variables and methods of any class, you can also call any object method at runtime, and create a new class object
at runtime. Look at the code below to understand what reflection is, and you don't need to recite
public class Base { static int num = 1; static { System.out.println("Base "+ num); } } public class Main { public static void main(String[] args) { // Will not initialize static block Class clazz1 = Base.class; System.out.println("------"); // Class will be initialized clazz2 = Class.forName("zzz.Base"); } }












12.jdk1.8 and 1.7 difference

I understand that the new features of JDK8 are: even ordinary methods can be added to the interface. It doesn't have to be abstract methods, but it must be decorated with default. When calling, it must be called after the interface is implemented, and there is Added a Lambda expression to let us traverse the collection data faster. I probably know these. We have not used these new features in our current development. By the way, there is also the map storage after JDK 1.8. The way is 1.7, there are some differences. If they are not equal, a linked list structure is formed. The one added after jdk1.7 is in front, and the one added first is removed. This situation is called collision. This kind of collision should be avoided as much as possible. If there is a large amount of data in the linked list in an index, the equals comparison will affect the efficiency when inserting an object again.
Therefore, jdk1.8 improves the appearance of this collision situation. The HashMap storage structure in jdk1.8 is formed by three data structures: array, linked list, and red-black tree. Red-black tree query deletion is fast and slow. Reference article https://blog.csdn.net/changhangshi/article/details/82114727

13.Jvm optimization?

In fact, I don’t think it’s very good to optimize this one. I’ll talk about it in a way that I understand. For example, we have to abide by our Java coding standards. For example, we must use static variables as little as possible. It is best not to exceed three levels of loop Nesting, because it is more troublesome to maintain in the later period, and it consumes JVM memory. Remove unnecessary jar packages in the project. If you feel that the JVM memory is not enough, you can modify catalina if your application runs to the tomcat server. The size of the Xms initialization heap under the bat file settings, and the maximum allowable allocation of heap memory in XXm. In fact, the larger the setting, the better, and it must be changed frequently. In my personal understanding, if the setting is larger, the garbage collection mechanism may not be Objects that are not long-used will be recycled in time.

14. The initialization process of the class

Student s = new Student(); What do you do in memory?
1. Load the Student.class file into memory
2. Open up space for s in stack memory
3. Open up space for student objects in heap memory
4. For student objects The member variables are initialized by default
5. The member variables of the student object are displayed and initialized
. 6. The member variables of the student object are assigned through the construction method.
7. The student object is initialized, and the object address is assigned to the s variable

15. Tell me about your understanding of Spring?

Regarding Spring, we have been using it in our daily projects, and we can integrate it whether we use ssh or ssm. There are three main points in Spring, that is, the core idea, IOC, DI, and AOP.
In fact, the spring framework also uses the reflection mechanism in Java.
DI is dependency injection, injecting the classes and interfaces we need into spring.
IOC inversion of control, like our previous development, if you want to create an object, just create a new one. If you want to define other variables or objects in this object, create a member variable inside the object. But now, if we want to use objects of this class, we can configure a bean in the spring configuration file and specify the corresponding full path name. Spring can directly help us obtain the objects of this class by using reflection through the configuration file. There is also AOP, which is aspect-oriented programming. For its principle, I have seen its underlying code. It actually implements the dynamic proxy of the JDK. In the past, we used this to control transactions. Now we all use annotations. Control affairs.
The AOP execution process is a vertical process, which treats each method as a point. Based on these points, it can be enhanced. A horizontal section is formed, including the original method and the enhanced method. The original code structure is not changed, but also added It has additional functions.
What are the usage scenarios of AOP you know? Transaction management, log printing, and it is also possible to use it for authority management in old projects. On the
whole, Spring is very convenient to use. It is more convenient to configure dependent objects in the configuration file, or inject objects and attributes in the configuration file. Of course, annotations are basically used now.
In addition to these, our previous projects have also used other spring products, such as spring boot (to simplify the initial setup and development process of new Spring applications. In my words, spring boot is actually not a new framework. It is configured by default. Many frameworks are used, like maven integrates all jar packages, spring boot integrates all frameworks), spring cloud microservice framework. Simpler, faster and more convenient than spring.
(Then you can talk about microservices, spring boot, cloud). . .

16.Spring common annotations

The commonly used annotations when we develop are also @service business logic, @Transactionnal transaction annotations, and sometimes we need to inject DAOs, @Repository and springMVC annotations are also used, such as @Controller annotations corresponding to the presentation layer In the future version of spring, the controller @RestController returns all in json format, and the URL entered by @RequestMapping, @responsebody also defines the method to return JSON format, and @RequestParam gets parameters, @RequestBody gets the front desk The data is data in JSON format.@PathVariable obtains data from the URL request path, and these are probably the most commonly used ones.

17. Briefly talk about how Spring MVC and Spring are integrated?

Simply speaking, the integration of springMVC in ssm is to configure the core controller of springMVC in web.xml: DispatcherServlet; it is to intercept the specified suffix; then configure the scanner in springMVC.xml, you can scan to these with @controller annotations Classes, now using springMVC are based and annotated development, like @service, @Repository @Requestmapping, @responsebody, these annotation tags, etc. are used during development, each annotation tag has its own role; it is also configured A view parser is mainly to uniformly configure the jump after processing. That's roughly it. How to use springMVC to get the data in the form? It can be obtained by keeping the parameter and the name value in the form consistent.

18. Talk about the implementation principle of springaop in detail

Its bottom layer is the aspect-oriented programming implemented by dynamic proxy, which is mainly used to manage things. We configure the entry point in the configuration file, such as the method starting with save/insert/update, etc. We open things and configure a REQUIRED beginning The features of things can also be used for log management, and permissions can also be achieved with aop, but now basically no one controls permissions like this, and all use the shiro framework to achieve permission management. Springaop is probably like this. Our company is What is used to manage things, should be managed in the form of annotations. The following content is to understand, you can say about
the description of spring AOP transactions: (difficulty)
set an expression in spring-common.xml first,
set those methods in the service, such as: add*, delete* , update* and other methods
begin transaction interception. We need to configure the propagation (propagation="REQUIRED")
feature of the transaction . Usually, operations other than addition, deletion, and modification need to be configured as read-only transactions (read
only="true"). Read-only transactions can improve performance. Then introduce tx:advice,
reference transactionManager (transaction management) in tx:advice , introduce
sessionFactory in transaction management , sessionFactory inject dataSource, and finally
introduce txAdvice.
Spring implements ioc Inversion of Control Description:
Originally, we needed to create and inject beans by ourselves, but now we leave it to the spring container to
complete the creation and injection of beans. The so-called "inversion of control" is the transfer of control rights of the object
, from the program code itself to the external container.

19. What is the difference between @Autowired and @Resource in Spring?

@Autowired default is to inject according to the type, if there is no type, it will be injected according to the name (red font).
If you want to directly inject according to the name, you need to add @Qualifier("gatheringDao")
@Autowired
@Qualifier("gatheringDao")
private GatheringDao gatheringDao ;
@Resource will inject according to the name by default. If the name is not found, it will be found according to the type. If the name is written here, it will be directly found by the name and will not be found by the type @Resource(name = “aaa”)
@Resource
private GatheringDao gatheringDao ;

20. The propagation characteristics of transactions?

1.PROPAGATION_REQUIRED: Support the current transaction, if there is no transaction currently, create a new transaction. This is the most common choice.
2.PROPAGATION_SUPPORTS: support the current transaction, if there is no transaction currently, it will be executed in a non-transactional manner.
3. PROPAGATION_MANDATORY: support the current transaction, if there is no current transaction, throw an exception.
4. PROPAGATION_REQUIRES_NEW: Create a new transaction, if there is a transaction currently, suspend the current transaction.
5. PROPAGATION_NOT_SUPPORTED: Perform the operation in a non-transactional manner. If there is a transaction currently, the current transaction is suspended.
6. PROPAGATION_NEVER: Execute in a non-transactional manner. If there is a transaction currently, an exception will be thrown.
7. PROPAGATION_NESTED: Support the current transaction, add Savepoint points, and submit or roll back synchronously with the current transaction.

Guess you like

Origin blog.csdn.net/zhang_yuanbai/article/details/108701783