Spring Framework_学习记录_12

1. The Autowired Annotation

  • Circle.java
package org.zcs.spring;

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

public class Circle implements Shape {

	private Point center;
	public Point getCenter() {
		return center;
	}
	@Autowired
	@Qualifier("circleRelated")
	public void setCenter(Point center) {
		this.center = center;
	}
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing Circle:");
		System.out.println("Circle : point is : " + center.getX() +","  + center.getY());
		
	}
	
}

  • DrwaingApp.java
package org.zcs.spring;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 

public class DrawingApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//BeanFactory factory = new  FileSystemXmlApplicationContext("src/spring.xml");
		//Triangle triangle = new Triangle();
		 ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
 
		Shape circle = (Shape)context.getBean("circle");
		circle.draw();
	}
}

  • spring.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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
 			<context:annotation-config  />
<!--    
   	<bean id = "triangle" class= "org.zcs.spring.Triangle"  autowire= "byName"  >
 		  
 		 <property name="pointA" ref = "pointA"></property>
 		 <property name="pointB" ref = "pointB"></property>
 		 <property name="pointC" ref = "pointC"></property>
 		
   	</bean> -->
   	
   	<bean id= "pointA" class = "org.zcs.spring.Point">

   		 <qualifier  value = "circleRelated"></qualifier>
   		<property name="x"  value = "${pointA.pointX}"></property>
   		<property name="y"  value = "${pointA.pointY}"></property>
   	</bean>
   	<bean id= "pointB" class = "org.zcs.spring.Point">
		<property name="x"  value = "20"></property>
		<property name="y"  value = "0"></property>
	</bean>

 	<bean id= "pointC" class = "org.zcs.spring.Point">
		   <property name="x"  value = "0"></property>
		   <property name="y"  value = "20"></property>
   	</bean>
 	
   	 <bean id = "circle" class= "org.zcs.spring.Circle"   >
 		 
   	</bean>
   	 	<bean class = "org.zcs.spring.MyBeanFactory" ></bean>
   	<bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   		<property name="locations" value = "pointsconfig.properties"></property>
   	</bean>
   	 <bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
   	 </bean>
   	
  </beans>
 

2.学习记录

  • 本次学习了spring框架中的Required和Autowired的用法
  • 其中Autowired使用了三种方法做了演示:
    • 在bean中只有一个poinA时,会自动识别它
    • 在bean中有多个point时,需要将它的名字改为和Autowired的注释的方法的名字相同
  • qualifier注释的使用。

3.错误记录

  • 在使用qualifier时出现报错,qualifer的信息总找不到。
    -解决办法:在beans中添加 <context:annotation-config />,并在头文件中添加contex的相关内容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值