Spring assembly Bean (explicitly configured in Java)

Source: Internet
Author: User

1. Brief Introduction

Spring provides three kinds of assembly mechanisms:

1. Explicit configuration in xml;

2. Explicit configuration in java;

3. Implicit bean discovery Mechanism and auto-assembly.

of these, 1 and 3 items are used frequently in projects, and display configuration in Java is rarely used. This article focuses on the 2nd Approach.

If in the project we need to assemble a third-party library into spring, We cannot use implicit assembly (annotations that cannot be added to the Third-party library @component, etc.),

You need to select the method configuration in both explicit configurations.

The way in which it is explicitly configured in Java is a better solution because it is more powerful, Type-safe and refactoring-friendly. And when you need to assemble a bean very much, put it in an XML configuration file

Inconvenient to manage, using Java configuration just put all the javaconfig under one package and scan this Package.

2. Code Implementation

1.applicationcontext-service.xml Scan Javaconfig Pack

1 <  base-package= "com.taozhiye.JavaConfig"></Context:component-scan  >

  

2.cdplayer.java

1  package com.taozhiye.JavaConfigTemp; 2 3  public Interface CDPlayer {4     5      public void Get (); 6 7 }

3.sgtpeppers.java

1  package com.taozhiye.JavaConfigTemp; 2 3  public class Implements CDPlayer {4    @Override5public     void  Get () {6         System.out.println ("sgtpeppers"); 7     }89 }

4.whitealbum.java

  1  package   com.taozhiye.JavaConfigTemp;   2   3  public  class  whitealbum implements   cdplayer {  4   5   @Override   6  public  void   get () {  7  System.out.println ("whitealbum"   8  }   9  10 } 

5.javaconfig.java need to scan the package of this file

1  packagecom.taozhiye.JavaConfig;2 3 Importorg.springframework.context.annotation.Bean;4 Importorg.springframework.context.annotation.Configuration;5 6 Importcom.taozhiye.JavaConfigTemp.CDPlayer;7 Importcom.taozhiye.JavaConfigTemp.SgtPeppers;8 Importcom.taozhiye.JavaConfigTemp.WhiteAlbum;9 Ten @Configuration one  public classJavaconfig { a      -@Bean (name = "cdplayer") -      publicCDPlayer get () { the         intChoice = (int) Math.floor (math.random ()); -System.out.println ("choice:" +choice); -         if(choice = = 0){ -             return Newsgtpeppers (); +}Else{ -             return Newwhitealbum (); +         } a     } at}

6.javaconfigaction.java

1  packagecom.taozhiye.controller;2 3 Importorg.springframework.beans.factory.annotation.Autowired;4 Importorg.springframework.stereotype.Controller;5 Importorg.springframework.web.bind.annotation.RequestMapping;6 Importorg.springframework.web.bind.annotation.ResponseBody;7 8 Importcom.taozhiye.JavaConfigTemp.CDPlayer;9 Ten  one @Controller a  public classjavaconfigaction { -      -@Autowired (required =false) the      publicCDPlayer cdplayer; -      -@RequestMapping ("getcdplayer") -      public@ResponseBody String getcdplayer () { + System.out.println (cdplayer); -         if(cdplayer!=NULL){ + Cdplayer.get (); a         } at         return"cdplayer"; -     } -}

This completes the explicit configuration in simple Java.

Spring assembly Bean (explicitly configured in Java)

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.