`
LaxLee
  • 浏览: 4648 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Spring use annotation

阅读更多
今天本来是要做spring+jpa的例子的但是奈何 老是报错 所以就开始研究spring的annotation的例子 顺便自己做了做 留在博客 以免自己忘记 老同志们看了 请绕开这个初级的博文
首先是配置文件
<?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">
          <context:component-scan base-package="com.lee"/>
</beans>

这是要注入的类内容

package com.lee.test;


import org.springframework.stereotype.Repository;
/**
* @Service用于标注业务层组件、 @Controller用于标注控制层组件(如struts中的action)、
* @Repository用于标注数据访问组件,即DAO组件。而@Component泛指组件,
* 当组件不好归类的时候,我们可以使用这个注解进行标注。
* @author Administrator
*
*/
@Repository
public class Test {

public void say(){
System.out.println("注解的方式");
}

}


package com.lee.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Service("xxx")//在扫描时可以自定义名字
@Scope("prototype")
public class Say {

private Test test;

@Autowired
public void setTest(Test test) {
this.test = test;
}

public void mySay(){
test.say();
}



}

再来个测试类

package com.lee.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Heh {
/**
* 正常的基于XML的setter模式
* <?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">
          <bean id="test" class="com.lee.test.Test"/>
          <bean id="say" class="com.lee.test.Say">
          <property name="test" ref="test"></property>
          </bean>
          </beans>
*
*/
@Test
public void testSay(){

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("say");
say.mySay();
}
/**
* 采用注解的形式
* 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">
          <context:annotation-config/>
          <bean id="test" class="com.lee.test.Test"/>
          <bean id="say" class="com.lee.test.Say"/>
          </beans>
*/

@Test
public void testAnnotation(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("say");
say.mySay();
}
/**
* 使用自动扫描的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">
          <context:component-scan base-package="com.lee"/>
</beans>
*/

@Test
public void testAuto(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Say say = (Say) ac.getBean("xxx");
Say say2 = (Say) ac.getBean("xxx");
System.out.println(say == say2);
say.mySay();
}
}

0
0
分享到:
评论

相关推荐

    annotation

    在IT行业中,"annotation"(注解)是一个关键的概念,特别是在Java编程语言中。注解是一种元数据,它提供了在不改变程序代码行为的情况下向编译器或JVM(Java虚拟机)提供信息的方式。注解可以用于简化开发、提供...

    spring-boot +MyBatis 框架集成

    import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.example.demo.mapper") public class MyBatisConfig { } ``` 5. **编写实体类**:根据数据库表结构创建...

    Java Annotation

    - **Spring Framework**:使用了多种注解来实现依赖注入、事务管理等功能。 - **Hibernate/JPA**:使用注解简化了实体映射的过程。 - **JUnit**:通过 `@Test`、`@Before`、`@After` 等注解来组织单元测试代码。 - *...

    Java自定义注解Annotation的使用

    ### Java自定义注解Annotation的使用 #### 1. 前言 自从JDK 1.5引入了注解这一特性以来,它已经成为Java开发中的一个重要组成部分。注解最初是为了推动EJB 3.0的普及和发展而设计的,其目的是减少配置文件的使用,...

    Getting.started.with.Spring.Framework.2nd.Edition1491011912.epub

    All the examples shown in this book use Spring 4. You can download the examples (consisting of 60 sample projects) described in this book from the following Google Code project: code.google....

    springBoot+springjpa.docx

    import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class QuickStartController { @RequestMapping("/quick") ...

    idea下整合springboot+spring data jpa

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @...

    spring-boot-reference.pdf

    Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. ...

    spring boot

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot....

    Using Spring from Scratch

    3. **AOP(面向切面编程)**:`Spring_1600_AOP_XML`和`Spring_1500_AOP_Annotation`涉及到Spring的AOP特性,它允许开发者定义横切关注点,如日志记录、事务管理等,这些关注点可以被编织到业务逻辑中,而无需侵入...

    springDataJpa测试demo

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringDataJpaDemoApplicationTests { @Autowired ...

    Spring boot基础搭建

    import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/") public class UserController { @...

    Spring boot集成Mybatis通用mapper

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.tjproject.entity.User; import com.example.tjproject.mapper.UserMapper; ...

    Spring+MyBatis含分页的基本配置

    &lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useSSL=false&amp;serverTimezone=UTC"/&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource....

    MyBatis与Spring整合——通过官方文档进行最简单的整合

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EmployeeService { private final EmployeeMapper employeeMapper...

    gralde+spring+springmvc+springjdbc+mysql

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { @Autowired ...

    spring boot中配置mybatis热加载.zip

    import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; ...

    Java spring boot链接mql数据库 JDBC

    import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class StudentController { @Autowired private StudentRepository studentRepository; @...

Global site tag (gtag.js) - Google Analytics