java xml annotation_Spring Configuration (XML, Annotation & Java)

First step to use Spring in your Java application, is to create Spring configuration metadata, this metadata tells Spring what beans “Java classes” you need to be instantiated in Spring container. You have three options to write or code your configuration metadata “XML, annotation or Java configuration”

1883f7d7183ae42b18175ab36d18a442.png

Objective

How to prepare Spring configuration metadata using XML, annotation or Java based configuration?

Environment & Tools:

Eclipse

Maven

Libraries:

Spring framework

List of jars

:

.classpath

List of dependencies

:

pom.xml

Here will see how to instantiate a bean “MyBean.java” using XML, annotation and Java configuration.

( 1 ) XML Configuration

This is the preferred way to configure Spring.

Beans:

These are the Java classes “beans” we want to create in Spring container.

src/main/java/com/hmkcode/spring/beans/

MyBean.java

package com.hmkcode.spring.beans;

public class MyBean {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "MyBean [name=" + name + "]";

}

}

src/main/java/com/hmkcode/spring/beans/

AnotherBean.java

package com.hmkcode.spring.beans;

public class AnotherBean {

private MyBean myBean;

public MyBean getMyBean() {

return myBean;

}

public void setMyBean(MyBean myBean) {

this.myBean = myBean;

}

@Override

public String toString() {

return "AnotherBean [myBean=" + myBean + "]";

}

}

Configuration:

src/main/resources/config/

XMLConfig.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd">

This equivalent to:

MyBean myBean = new MyBean();

myBean.setName("xmlBean");

AnotherBean anotherBean = new AnotherBean();

anotherBean.setMyBean(mybean);

Run:

package com.hmkcode;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hmkcode.spring.beans.AnotherBean;

import com.hmkcode.spring.beans.MyBean;

public class App

{

public static void main( String[] args )

{

ApplicationContext ctxXML = new ClassPathXmlApplicationContext("config/XMLConfig.xml");

AnotherBean anotherBean = (AnotherBean) ctxXML.getBean("anotherBean");

System.out.println( anotherBean);

}

}

Output

:

AnotherBean [myBean=MyBean [name=xmlBean]]

( 2 ) Annotation-Based Configuration

Beans + Annotation:

src/main/java/com/hmkcode/spring/beans/

MyBean.java

@Component

(value=

“myBean2″

) : this will enable Spring to detect this bean when scanning and instantiate a bean named “

myBean2

@Value

(value=

“AnnotationBean”

) : this equivalent to mybean2.setName(“AnnotationBean”);

package com.hmkcode.spring.beans;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

@Component(value="myBean2")

public class MyBean {

private String name;

public String getName() {

return name;

}

@Value(value="AnnotationBean")

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "MyBean [name=" + name + "]";

}

}

src/main/java/com/hmkcode/spring/beans/

AnotherBean.java

@Component

(value=

“anotherBean”

) : this will enable Spring to detect this bean when scanning and instantiate a bean named “anotherBean”

@Autowired

@Qalifier

(

“myBean2″

): this equivalent to anotherBean.setMyBean(mybean2)

package com.hmkcode.spring.beans;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Component;

@Component(value="anotherBean")

public class AnotherBean {

private MyBean myBean;

public MyBean getMyBean() {

return myBean;

}

@Autowired

@Qualifier("myBean2")

public void setMyBean(MyBean myBean) {

this.myBean = myBean;

}

@Override

public String toString() {

return "AnotherBean [myBean=" + myBean + "]";

}

}

XML Configuration:

to enable annotation

We still need the XML just to enable annotation.

src/main/resources/config/

XMLConfig-Annotation.xml

Run:

package com.hmkcode;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hmkcode.spring.beans.AnotherBean;

public class App

{

public static void main( String[] args )

{

ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("config/XMLConfig-Annotation.xml");

AnotherBean anotherBean = (AnotherBean) ctxAnnotation.getBean("anotherBean");

System.out.println( anotherBean);

}

}

Output:

AnotherBean [myBean=MyBean [name=AnnotationBean]]

( 3 ) Java-Based Configuration

Beans:

src/main/java/com/hmkcode/spring/beans/

MyBean.java

package com.hmkcode.spring.beans;

import org.springframework.beans.factory.annotation.Value;

public class MyBean {

private String name;

public String getName() {

return name;

}

@Value(value="JavaConfigBean")

public void setName(String name) {

this.name = name;

}

@Override

public String toString() {

return "MyBean [name=" + name + "]";

}

}

src/main/java/com/hmkcode/spring/beans/

AnotherBean.java

package com.hmkcode.spring.beans;

import org.springframework.beans.factory.annotation.Autowired;

public class AnotherBean {

private MyBean myBean;

public MyBean getMyBean() {

return myBean;

}

@Autowired

public void setMyBean(MyBean myBean) {

this.myBean = myBean;

}

@Override

public String toString() {

return "AnotherBean [myBean=" + myBean + "]";

}

}

Cofiguration:

package com.hmkcode.spring.config;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import com.hmkcode.spring.beans.AnotherBean;

import com.hmkcode.spring.beans.MyBean;

@Configuration

public class JavaConfig {

@Bean

public MyBean myBean(){

return new MyBean();

}

@Bean(name = "anotherBean2")

public AnotherBean anotherBean(){

return new AnotherBean();

}

}

Run:

package com.hmkcode;

import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.hmkcode.spring.beans.AnotherBean;

import com.hmkcode.spring.config.JavaConfig;

public class App

{

public static void main( String[] args )

{

ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);

AnotherBean anotherBean = (AnotherBean) ctxJavaConfig.getBean("anotherBean2");

System.out.println( anotherBean);

}

}

Output:

AnotherBean [myBean=MyBean [name=JavaConfigBean]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值