Home  >  Article  >  Java  >  Detailed introduction to Spring4

Detailed introduction to Spring4

零下一度
零下一度Original
2017-07-20 19:09:272404browse

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:

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

public interface MessageService {
	String getMessage();
}

package com.mm.service.impl;

import com.mm.service.MessageService;

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

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();
    }
}

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());
    }
}

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();
	}
}

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();//    }}

Add annotations to MessageServiceImpl

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

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!

Statement:
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