spring boot01,spring完全注解(零配置)编程方式
Posted chao123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot01,spring完全注解(零配置)编程方式相关的知识,希望对你有一定的参考价值。
前言:
spring boot就是整合了很多优秀的框架,不用我们自己手动的去写一堆xml配置然后进行配置。有了它, 你可以更加敏捷地开发Spring应用程序,专注于应用程序的功能,不用在Spring的配置上多花功 夫,甚至完全不用配置。
一 spring4.x的java注解方式编程
Java注解是Spring4.x推荐的配置方式,可以完全替代xml配置。Spring的Java注解方式是通过 @Configuration和 @Bean这两个注解实现的:
1、@Configuration 作用于类上,相当于一个xml配置文件;
2、@Bean 作用于方法上,相当于xml配置中的<bean>;
上案例!!!
开发环境:spring4.3.9+jdk1.7+maven3.6+eclipse mars.2+mysql
结构图:
pom.xml配置如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.lzq</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <!-- jar包统一升级管理 --> <spring.version>4.3.9.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <!-- 日志包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- mysql连接驱动包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> </project>
实体类:UserEntity.java
package org.lzq.entity; import java.io.Serializable; public class UserEntity implements Serializable { private static final long serialVersionUID = -5425731832559669953L; private Integer userId; private String userName; private String passWord; public UserEntity() { super(); } public UserEntity(Integer userId, String userName, String passWord) { super(); this.userId = userId; this.userName = userName; this.passWord = passWord; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassWord() { return passWord; } public void setPassWord(String passWord) { this.passWord = passWord; } }
dao层代码:
IUserDao.java
package com.accp.dao; import java.util.List; import org.lzq.entity.UserEntity; public interface IUserDao { //查询所有 List<UserEntity> queryAll(); }
UserDaoImpl.java
package com.lyc.dao; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Repository; import com.lyc.entity.UserEntity; @Repository("userDao") public class UserDaoImpl extends BaseDao implements IUserDao{ @Override public List<UserEntity> queryAll() { List<UserEntity> list = new ArrayList<UserEntity>(); String sql= "select * from users"; rs = executeQuery(sql); try { UserEntity user=null; while(rs.next()){ user=new UserEntity(); user.setUserId(rs.getInt("userId")); user.setUserName(rs.getString("userName")); user.setPassWord(rs.getString("passWord")); list.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ closeAll(); } return list; } }
业务层代码
UserService.java
package com.lyc.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.lyc.dao.IUserDao; import com.lyc.entity.UserEntity; @Service("userService") public class UserService { //Autowired默认根据类型进行装配,加上Qualifier注解则变成根据名称装配 @Autowired @Qualifier("userDao") private IUserDao userDao; public List<UserEntity> queryAll() { return userDao.queryAll(); } }
配置类
SpringConfig.java
package com.lyc.configs; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import com.lyc.dao.IUserDao; import com.lyc.dao.UserDaoImpl; //通过该注解来表明该类是一个Spring的配置,相当于一个传统的ApplicationContext.xml @Configuration //相当于配置文件里面的<context:component-scan/>标签 @ComponentScan(basePackages="com.lyc.dao,com.lyc.service") public class SpringConfig { // 通过该注解来表明是一个Bean对象,相当于xml中的<bean> //bean的id值默认是方法名userDao /* @Bean public IUserDao userDao(){ return new UserDaoImpl(); } */ }
测试类代码
Test01.java
package com.lyc.test; import java.util.List; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.lyc.configs.SpringConfig; import com.lyc.entity.UserEntity; import com.lyc.service.UserService; public class Test01 { public static void main(String[] args) { //通过java配置来实例化spring容器 AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(SpringConfig.class); //在spring容器中获取bean对象 //名称userService是注解根据类UserService自动生成的 UserService userService = (UserService) acac.getBean("userService"); //或者采取下面的方式 //UserService userService = acac.getBean(UserService.class); List<UserEntity> list = userService.queryAll(); for (UserEntity u : list) { System.out.println(u.getUserName()+"---"+u.getPassWord()); } //销毁容器 acac.destroy(); } }
感觉还是挺简单的哈!!!
以上是关于spring boot01,spring完全注解(零配置)编程方式的主要内容,如果未能解决你的问题,请参考以下文章
Spring+Spring Boot+Mybatis框架注解解析
spring boot框架学习学前掌握之重要注解-java配置方式