欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring(注解开发)

程序员文章站 2022-03-05 12:05:11
...

先来说说注解是什么:

注解顾名思义,就是类似于添加注释,但是又不跟注释完全一样,因此,我们可以将它理解为将类或者方法与特定的信息进行关联。
那么注解如何实现?

1、接口使用@interface定义。

2、通过继承以**解,实现功能:

元注解@Target,@Retention,@Documented,@Inherited

* 元注解@Target,@Retention,@Documented,@Inherited * 
* @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括: 
* ElemenetType.CONSTRUCTOR 构造器声明 
* ElemenetType.FIELD 域声明(包括 enum 实例) 
* ElemenetType.LOCAL_VARIABLE 局部变量声明 
* ElemenetType.METHOD 方法声明 
* ElemenetType.PACKAGE 包声明 
* ElemenetType.PARAMETER 参数声明 
* ElemenetType.TYPE 类,接口(包括注解类型)或enum声明 
* 
* @Retention 表示在什么级别保存该注解信息。可选的 RetentionPolicy 参数包括: 
* RetentionPolicy.SOURCE 注解将被编译器丢弃 
* RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃 
* RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。 
* 
* @Documented 将此注解包含在 javadoc 中 
* 
* @Inherited 允许子类继承父类中的注解


@Target(ElementType.METHOD) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Inherited 

下面拿spring的controller来当做示例:

     Controller类使用继承@Component注解的方法,将其以单例的形式放入spring容器,如果仔细看的话会发现每个注解里面都有一个默认的value()方法,它的作用是为当前的注解声明一个名字,一般默认为类名,然后spring会通过配置文件中的<context:component-scan>的配置,进行如下操作:

1、使用asm技术扫描.class文件,并将包含@Component及元注解为@Component的注解@Controller、@Service、@Repository或者其他自定义的的bean注册到beanFactory中,

2、然后spring在注册处理器

3、实例化处理器,然后将其放到beanPostFactory中,然后我们就可以在类中进行使用了。

4、创建bean时,会自动调用相应的处理器进行处理。

这里附上参考文章https://blog.csdn.net/jack_wang001/article/details/78781588

@Controller源码:

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.springframework.stereotype;
 
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;
 
/**
 * Indicates that an annotated class is a "Controller" (e.g. a web controller).
 *
 * <p>This annotation serves as a specialization of {@link Component @Component},
 * allowing for implementation classes to be autodetected through classpath scanning.
 * It is typically used in combination with annotated handler methods based on the
 * {@link org.springframework.web.bind.annotation.RequestMapping} annotation.
 *
 * @author Arjen Poutsma
 * @author Juergen Hoeller
 * @since 2.5
 * @see Component
 * @see org.springframework.web.bind.annotation.RequestMapping
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {
 
	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";
 
}

下面是@Component的源码:

/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.springframework.stereotype;
 
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;
 
/**
 * Indicates that an annotated class is a "component".
 * Such classes are considered as candidates for auto-detection
 * when using annotation-based configuration and classpath scanning.
 *
 * <p>Other class-level annotations may be considered as identifying
 * a component as well, typically a special kind of component:
 * e.g. the {@link Repository @Repository} annotation or AspectJ's
 * {@link org.aspectj.lang.annotation.Aspect @Aspect} annotation.
 *
 * @author Mark Fisher
 * @since 2.5
 * @see Repository
 * @see Service
 * @see Controller
 * @see org.springframework.context.annotation.ClassPathBeanDefinitionScanner
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
 
	/**
	 * The value may indicate a suggestion for a logical component name,
	 * to be turned into a Spring bean in case of an autodetected component.
	 * @return the suggested component name, if any
	 */
	String value() default "";
 
}

使用Java Web 注解开发可以不用不用任何xml配置文件来开发Java Web。
大致原理是:

实现ServletContainerInitializer接口
在META-INF/service 建立spi文件(spi机制),指定ServletContainerInitializer的实现类
在实现类上加@HandlesTypes 注解(Tomcat扫到该注解的指定的类型以及该类型的泛华类型)
实现类onStartup的方法参数Set<Class<?>> c 就是@HandlesTypes指定类型的泛化类型全部的类,由Tomcat容器扫描到的进来
项目启动Tomcat会扫描实现ServletContainerInitializer的类,并扫描实现类上@HandlesTypes指定的类型和泛化类型全部的类传到实现类的onStartup方法上
在实现类的onStartup里面注册filter,listener,servlet

@HandlesTypes({Handler.class})
public class SampleServletContainerInitializer implements ServletContainerInitializer {

@Override
public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {

    // c servlet 容器帮我们注入感兴趣的类型

    ServletRegistration.Dynamic homeServlet = ctx.addServlet("home", HomeServlet.class);
    homeServlet.addMapping("/home");

    // 注册过滤器
    FilterRegistration.Dynamic userFilter = ctx.addFilter("userFilter", UserFilter.class);
    userFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");

    // 注册监听
    ctx.addListener(UserListener.class);

}

}