Spring annotation 使用

本文详细介绍了如何在Spring框架中使用注解实现对Hibernate的集成应用,包括配置SessionFactory、自定义类集成HibernateDaoSupport以及在Spring配置文件中的正确设置。

测试了spring annotation的使用,

服务端可以用@Repository,@Service,@Controller

注入使用@Resource,@Autowired,建议使用@Resource

项目使用了hibernate,因为在sessionFactory也需要注入,自定义一个类

集成HibernateDaoSupport,与数据库打交道的都继承自定义的这个类

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class CustomHibernateDaoSupport extends HibernateDaoSupport{
	@Resource
    public void anyMethodName(SessionFactory sessionFactory)
    {
        setSessionFactory(sessionFactory);
    }
}

 在spring中配置也要注意

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
               
	<!-- Database Configuration -->
	<import resource="config/spring/DataSource.xml" />
	<import resource="config/spring/HibernateSessionFactory.xml" />

	<!-- Beans Declaration -->
	<import resource="com/note/customer/spring/CustomerBean.xml" />

	<!--没有这句出错,找不到 -->
	<context:component-scan base-package="com.note" />
</beans>
 

例子请看原文:

http://www.mkyong.com/spring/maven-spring-hibernate-annotation-mysql-example/

### 如何在Spring框架中获取AnnotationMetadata 在Spring框架中,`AnnotationMetadata`接口用于描述类级别的元数据。这允许开发人员访问目标类型的注解信息而不必实例化该类型。对于基于Java配置或扫描组件的应用程序来说非常有用。 为了获得某个特定bean定义的`AnnotationMetadata`对象,可以利用`StandardReflectionMetadataReaderFactory`来读取并处理这些信息[^1]: ```java import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AnnotationTypeFilter; public class MetadataExample { private final MetadataReaderFactory metadataReaderFactory; public void readAnnotations(String className) throws IOException { MetadataReader reader = metadataReaderFactory.getMetadataReader(className); AnnotationMetadata annotationMetadata = reader.getAnnotationMetadata(); // 判断是否存在指定注解 boolean hasRestcontroller = annotationMetadata.hasAnnotation(RestController.class.getName()); System.out.println("Has RestController: " + hasRestcontroller); // 获取所有注解名称 Set<String> annotations = annotationMetadata.getAnnotationTypes(); for (String anno : annotations) { System.out.println(anno); } } } ``` 此外,在某些情况下可能需要通过实现自定义逻辑来间接取得此类信息。例如当使用AOP代理时,可以通过拦截器或者后处理器的方式捕获到被增强的目标对象及其关联的元数据[^4]。 如果是在编写测试案例或是工具性质的功能模块,则可以直接借助于反射API配合Spring提供的辅助方法来进行操作。比如下面这段代码展示了如何直接从已知class对象提取其上的注解详情: ```java import org.springframework.core.annotation.AnnotatedElementUtils; public static Map<String, Object> getClassLevelAnnotations(Class<?> clazz){ MultiValueMap<String, Object> attributes = AnnotatedElementUtils.getAllAnnotationAttributes(clazz, true, false); return CollectionUtils.isEmpty(attributes)? Collections.emptyMap():attributes.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().get(0))); } ``` 以上两种方式分别适用于不同场景下的需求,前者更侧重于编译期静态分析,后者则适合运行时期动态查询。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值