Simple Spring annotation Injection Using Reflection

Source: Internet
Author: User
Tags custom name

Simple Spring annotation Injection Using Reflection

On the Rise of the moment, I looked at the source code of Spring over the past two days and wanted to write a simple Spring Injection Using annotation !! Spring annotation injection gives us convenient advantages! Everyone should understand the injection principles of Spring. Here we will write a very simple example of Injection Using annotations and take a small note!

To use annotations, it is absolutely inseparable from reflection. Excerpt

Reflection is one of the characteristics of the Java programming language. It allows running Java programs to check themselves, or "self-Review", and can directly operate on internal properties of the program. For example, you can use it to obtain and display the names of members in the Java class. This capability of Java may not be used in many practical applications, but it does not exist in other programming languages. For example, Pascal, C, or C ++ cannot obtain function definition-related information in the program.

Reflection can make our annotations more flexible.

Let's first look at a judgment annotation to determine whether this class can be used with our annotation, just like SpringMVC @ controller

Package org. xie. annotation. device; import java. lang. annotation. documented; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target;/*** used to determine whether the class can be used to inject * @ author cool, program to go **/@ Target (ElementType. TYPE) @ incluented @ Retention (RetentionPolicy. RUNTIME) public @ interface IsAnnotation {}

Then there is the annotation we will use.

Package org. xie. annotation. device; import java. lang. annotation. documented; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; @ brief ented @ Target (ElementType. METHOD) @ Retention (RetentionPolicy. RUNTIME) public @ interface SetterAnnotation {// custom annotation public Class nation ();}
In this annotation, a Class object is returned. Here we do not inject a Class through a custom name like spring, but directly inject it through the Class type .... Is a small example

Then there is the interface we will use.

package org.xie.Interface;public interface IUser {void login();}


And two implementation classes

First, Chinese

Package org. xie. interface. impl; import org. xie. interface. IUser; public class ChineseUserImpl implements IUser {public void login () {System. out. println ("injecting Chinese ");};}

Then the English

package org.xie.Interface.impl;import org.xie.Interface.IUser;public class EnglishUserImpl implements IUser {@Overridepublic void login() {// TODO Auto-generated method stubSystem.out.println("English");}}

Next, let's take a look at the information of the class we want to inject.

package org.xie.relfect.first;import org.xie.Interface.IUser;import org.xie.Interface.impl.ChineseUserImpl;import org.xie.Interface.impl.EnglishUserImpl;import org.xie.annotation.device.AnnotationTest;import org.xie.annotation.device.IsAnnotation;import org.xie.annotation.device.SetterAnnotation;@IsAnnotationpublic class SetterBean {private IUser userdao;@SetterAnnotation(nation=EnglishUserImpl.class)public void setUserdao(IUser userdao) {this.userdao = userdao;}public void login_Test(){userdao.login();}}


Next is our annotation parsing class.

Package org. xie. relfect. first; import java. lang. reflect. method; import java. nio. channels. seekableByteChannel; import org. xie. interface. IUser; import org. xie. interface. impl. chineseUserImpl; import org. xie. annotation. device. isAnnotation; import org. xie. annotation. device. setterAnnotation;/*** similar to spring container * @ author Administrator **/public class SpringWine {public static SetterBean getBean () {SetterBean bean = new SetterBean (); boolean isAnnotation = SetterBean. class. isAnnotationPresent (IsAnnotation. class); if (isAnnotation) {Method [] method = SetterBean. class. getDeclaredMethods (); for (Method method2: method) {if (method2.isAnnotationPresent (SetterAnnotation. class) {SetterAnnotation setterAnnotation = method2.getAnnotation (SetterAnnotation. class); System. out. println ("AnnotationTest (field =" + method2.getName () + ", nation =" + setterAnnotation. nation () + ")"); try {Class
 Clazz = setterAnnotation. nation (); IUser iuser = (IUser) clazz. newInstance (); bean. setUserdao (iuser); // return bean;} catch (Exception e) {// TODO: handle has tione. printStackTrace (); return null ;}}} return bean; // return null ;}}

Test class

package org.xie.relfect.first;import javax.swing.Spring;public class Test {public static void main(String[] args) {SetterBean bean=SpringWine.getBean();bean.login_Test();}}


You can get different results by changing different annotations. Of course, there are many things to optimize in this small example .... If you want to make it like Spring, it's still a bit of work to do.








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.