Getting started with spring's Java configuration (one of Spring boot learning)

Source: Internet
Author: User

Spring Java Configuration 1, creating a MAVEN project

Create a MAVEN project using idea, by the way, idea is really better than eclipse and familiar. Then MAVEN is the most mainstream of Java project management tools, first configure a try, very good to get started.

2. Import Dependency

Edit Pom File

1, first configure the JDK version and code, two ways, according to any one of the reasons can be, out of the question if both of them, as follows

<build>
<finalName>spring-learn</finalName>
<!--specify JDK version and project code--
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
</plugin>
</plugins>
</build>

<!--specify JDK version and project code--
<properties>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

2, import dependency, here only need spring's core dependency, if it is a Web project, need to add log dependencies

<!--Spring Core dependency--
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>

3. Creating beans

1, first show the directory structure (hint, in the creation of the "Java" directory, you need to right-click, make Dictionnary as--Source root, so as to build the package)

Description

Functionservice is a function bean used to be invoked.

Usefunctionservice is the bean that calls Functionservice

Javaconfig is the Java configuration of spring

Main is the running portal where the test configuration is in effect

2. The Functionservice code is as follows:


/**
Define a bean to be used
*/
public class Functionservice {
SayHello (String word) {
"Hello" +word+"!" ;
}
}

The Usefunctionservice code is as follows:

/**
*Defines a function class that uses thebean
*/
public class Usefunctionservice {
Functionservice Functionservice
public void Setfunctionservice (Functionservice functionservice) {
this. Functionservice = Functionservice "

public String SayHello (String word) {
return Functionservice.sayhello (word) }
} /span>

This creates two beans, but in order for them to be managed by spring, we need to do the following.

4. Configuration

The Javaconfig code is as follows

/**
*Configuration file
*/

//Use@ConfigurationAnnotations indicate that a configuration class is currently
@Configuration
public class Javaconfig {

@BeanNote indicates that the current method return value is aBean,BeanName is the method name
@Bean
Public FunctionserviceFunctionservice () {
return new Functionservice ();
}

@Bean
call Functionservice () directly when injecting the Functionservice Bean
Public Usefunctionservice Usefunctionservice () {
Usefunctionservice Usefunctionservice = new Usefunctionservice ();
Usefunctionservice.setfunctionservice (Functionservice ());
return Usefunctionservice;
}
}
5. Operation

The main code is as follows:


/**
*Run the Portal
*/
public class Main {
public static void main (string[] args) {
Annotationconfigapplicationcontext context =
new Annotationconfigapplicationcontext (Javaconfig. Class)
Usefunctionservice Usefunctionservice = Context.getbean (Usefunctionservice. Class) System. out.println (Usefunctionservice.sayhello ( "java config")) ;

Context.close () }
} /span>

Operation Result:

6. Summary

In the end, we have a brief summary of the simplest Java spring configuration in the upper program, although the usefulness of the actual project does not help much, but as a starting point for Java's spring configuration, we can understand how Java is configured. With further learning from behind, we will gradually deepen and master the spring configuration based on annotations and Java.

Getting started with spring's Java configuration (one of Spring boot learning)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.