spring之 bean的注解

注解就是将原来的xml配置的方式改用了@XXXX方式来代替

首先定义一个Ponse 实体类

package com.bean;

public class Ponse {
	private String name;
	private int age;
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Ponse(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public Ponse() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}

配置xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--id:唯一的别名,class:包路径+类名 -->
    <bean id="posnse" class="com.bean.Ponse">
    <!--name:实体类中的name属性,value:给name赋的值 -->
        <property name="name" value="jion"></property>
    </bean>
</beans>

测试类

package com;

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

import com.bean.Ponse;
import com.config.MianConfig;

public class Test {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
		Ponse ponse = (Ponse) applicationContext.getBean("posnse");
		System.out.println(ponse.getName());
	}
}

输出的结果:
在这里插入图片描述
注解版
先定义一个注解类 MianConfig

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.bean.Ponse;

@Configuration   //告诉spring这是一个配置类
public class MianConfig {
	@Bean("ponse")   //给容器注册一个bean,默认情况是用方法名来作为bean的id,可以在bean后指定id
	public Ponse getPonse() {
		// TODO Auto-generated method stub
		return new Ponse("你好",12);
	}
}

测试类

package com;

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

import com.bean.Ponse;
import com.config.MianConfig;

public class Test {
	public static void main(String[] args) {
		//这里要注意一下//AnnotationConfigApplicationContext(MianConfig.class)这是一个注解类
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MianConfig.class);
		Ponse ponse = (Ponse)applicationContext.getBean("ponse");
		System.out.println(ponse.getName());
		//获取bean的类型
		String[] naStrings = applicationContext.getBeanNamesForType(Ponse.class);
		for (String string : naStrings) {
			System.out.println(string);
		}
	}
}

结果:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值