今天本来是要做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();
}
}
分享到:
相关推荐
在IT行业中,"annotation"(注解)是一个关键的概念,特别是在Java编程语言中。注解是一种元数据,它提供了在不改变程序代码行为的情况下向编译器或JVM(Java虚拟机)提供信息的方式。注解可以用于简化开发、提供...
import org.springframework.context.annotation.Configuration; @Configuration @MapperScan("com.example.demo.mapper") public class MyBatisConfig { } ``` 5. **编写实体类**:根据数据库表结构创建...
- **Spring Framework**:使用了多种注解来实现依赖注入、事务管理等功能。 - **Hibernate/JPA**:使用注解简化了实体映射的过程。 - **JUnit**:通过 `@Test`、`@Before`、`@After` 等注解来组织单元测试代码。 - *...
### Java自定义注解Annotation的使用 #### 1. 前言 自从JDK 1.5引入了注解这一特性以来,它已经成为Java开发中的一个重要组成部分。注解最初是为了推动EJB 3.0的普及和发展而设计的,其目的是减少配置文件的使用,...
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....
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class QuickStartController { @RequestMapping("/quick") ...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @...
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. ...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot....
3. **AOP(面向切面编程)**:`Spring_1600_AOP_XML`和`Spring_1500_AOP_Annotation`涉及到Spring的AOP特性,它允许开发者定义横切关注点,如日志记录、事务管理等,这些关注点可以被编织到业务逻辑中,而无需侵入...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class SpringDataJpaDemoApplicationTests { @Autowired ...
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/") public class UserController { @...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.example.tjproject.entity.User; import com.example.tjproject.mapper.UserMapper; ...
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=UTC"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource....
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class EmployeeService { private final EmployeeMapper employeeMapper...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/users") public class UserController { @Autowired ...
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; ...
import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class StudentController { @Autowired private StudentRepository studentRepository; @...