Home > Java > javaTutorial > body text

Detailed introduction to Spring4

零下一度
Release: 2017-07-20 19:09:27
Original
2390 people have browsed it

The demo of this article is based on the spring official website entry case. Of course, I made some changes.

My maven web project structure is as follows:

Add spring dependencies in pom.xml:

接下来我们开始创建需要用到的类:
Copy after login
package com.mm.service;

public interface MessageService {
	String getMessage();
}
Copy after login

package com.mm.service.impl;

import com.mm.service.MessageService;

public class MessageServiceImpl implements MessageService{
	@Override
	public String getMessage() {
		return "hello mm";
	}
}
Copy after login

I don’t need injection in the form of spring configuration file, so I created a new configuration file class

package com.mm.main.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.mm.service.MessageService;
import com.mm.service.impl.MessageServiceImpl;

@Configuration
@ComponentScan(basePackages = "com.mm")
public class ApplicationConfig {
    @Bean
    MessageService messageService() {
        return new MessageServiceImpl();
    }
}
Copy after login

Create a new bean to call the service

package com.mm.main.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mm.service.MessageService;

@Component
public class MessagePrinter {
	@Autowired
	private MessageService messageService;
	public void printMessage() {
        System.out.println(this.messageService.getMessage());
    }
}
Copy after login

Finally complete the writing of the client

package com.mm.main.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Client {
	public static void main(String[] args) {
		ApplicationContext context=new AnnotationConfigApplicationContext(ApplicationConfig.class);
		MessagePrinter messagePrinter=context.getBean(MessagePrinter.class);
		messagePrinter.printMessage();
	}
}
Copy after login

The background print is as follows:

Next we use annotations to inject, first remove the MessageService bean in the configuration file class

@Configuration
@ComponentScan(basePackages = "com.mm")public class ApplicationConfig {//    @Bean//    MessageService messageService() {//        return new MessageServiceImpl();//    }}
Copy after login

Add annotations to MessageServiceImpl

@Service//注解方式public class MessageServiceImpl implements MessageService{
    @Overridepublic String getMessage() {return "hello mm";
    }
}
Copy after login

The same complete output

The above is the detailed content of Detailed introduction to Spring4. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!