深圳商城手机网站建设wordpress for sae 插件
web/
2025/9/29 13:32:36/
文章来源:
深圳商城手机网站建设,wordpress for sae 插件,cms开发,wordpress缩略图外链本篇讲的内容主要是基于Spring v2.5的注解来完成bean的定义 之前都是使用纯配置的方式来定义的bean 文章目录 前言1. Spring v2.5 注解开发定义bean第一步#xff1a;在需要定义的类上写上注解Component第二步#xff1a;在Spring Config中定义扫描包第三步#xff1a;主方法…本篇讲的内容主要是基于Spring v2.5的注解来完成bean的定义 之前都是使用纯配置的方式来定义的bean 文章目录 前言1. Spring v2.5 注解开发定义bean第一步在需要定义的类上写上注解Component第二步在Spring Config中定义扫描包第三步主方法中测试基于Component的衍生注解 2. Spring v3.0 纯注解开发第一步创建配置类第二步导入配置类 3. Bean的作用范围(单例/多例)与生命周期Bean的作用范围Bean的生命周期 4.依赖注入引用类型注入Autowire属性默认按类型注入按类型注入反射结果不唯一按名注入按名注入的标准写法少用 简单类型注入引入外部的properties文件 5. 第三方Bean管理依赖注入简单类型注入引用类型注入 6. XML配置比对注解配置 前言
XML配置解耦而注解是简化开发
1. Spring v2.5 注解开发定义bean
之前都是使用纯配置的方式在Spring Config中通过bean idxxx classxxx /来定义的bean从此处开始将根据注解开发定义bean
第一步在需要定义的类上写上注解Component
这里有两种写的形式 Component 或者是给其定义名称 Component(bookDao) 通过第一种形式就通过在getBean()方法中写类型来获得bean 通过第二种形式就通过在getBean()方法中写名称来获得bean
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.stereotype.Component;Component
// Component(bookDao)
public class BookDaoImpl implements BookDao {Overridepublic void save() {System.out.println(Book Dao Save...);}
}第二步在Spring Config中定义扫描包
注意这里也要开启命名空间 然后通过context:component-scan base-package.... /进行定义扫描的包这里就是扫描com.example.demo.dao下边的包修改这个包为com.example.demo也是可以的
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdcontext:component-scan base-packagecom.example.demo.dao //beans第三步主方法中测试
package com.example.demo;import com.example.demo.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class DemoApplication {public static void main(String[] args) {ApplicationContext ctx new ClassPathXmlApplicationContext(applicationContext.xml);// 第一种定义形式写法定义Component写BookDao bookDao ctx.getBean(BookDao.class);System.out.println(bookDao);// 第二种定义形式写法定义Component(bookDao)写BookDao bookDao1 (BookDao) ctx.getBean(bookDao);System.out.println(bookDao1);}
}结果均为
com.example.demo.dao.impl.BookDaoImpl3a52dba3基于Component的衍生注解
Controller表现层定义bean Service业务层定义bean Repository数据层定义bean 比如在BookServiceImpl类上就使用Service进行定义在BookDaoImpl类上就使用Repository进行定义加括号的规则Component类似调用的方式也和原本的类似 2. Spring v3.0 纯注解开发
既然是纯注解开发就需要删除之前冗余的这个配置文件纯注解开发将配置的xml文件转换为了一个配置类
第一步创建配置类
配置类写法为实际上就是用两个注解来替代了原本配置文件中的内容
package com.example.demo.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;Configuration
ComponentScan(com.example.demo)
public class SpringConfig {
}配置文件中的内容分别被配置类中的内容替代如下 假如想扫描多个包使用{}来写即可如ComponentScan({com.example.demo.dao, com.example.demo.service})
第二步导入配置类
将原本的加载配置文件的ClassPathXmlApplicationContext换成了加载配置类AnnotationConfigApplicationContext里面的参数是注解类名称.class即读取这个注解类剩下getBean的操作和原来一样。
package com.example.demo;import com.example.demo.config.SpringConfig;
import com.example.demo.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DemoApplication {public static void main(String[] args) {ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao ctx.getBean(BookDao.class);System.out.println(bookDao);}
}3. Bean的作用范围(单例/多例)与生命周期
Bean的作用范围
之前配置Bean的作用范围是在配置文件里通过指定scope属性定义的 scopesingleton/prototype
bean idbookDao namedao classcom.example.demo231116.dao.impl.BookDaoImpl scopeprototype /因为Bean默认是单例模式如果我们想要修改成多例模式就在类上加Scope注解括号的内容写prototype为多例singleton为单例
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;Repository
Scope(prototype)
public class BookDaoImpl implements BookDao {Overridepublic void save() {System.out.println(Book Dao Save...);}
}Bean的生命周期
之前提到Bean的生命周期其实就是我们可以定义在Bean加载的时候初始化一些资源在销毁前销毁一些资源在配置文件中写初始化和销毁是通过init-method和destroy-method实现的
bean idbookDaoCycle classcom.example.demo231116.dao.impl.BookDaoImpl init-methodinit destroy-methoddestroy /在注解开发中我们只需要在类中写好初始化和销毁方法并分别加上PostConstruct注解后执行init方法和PreDestroy销毁前执行destroy方法两个注解注意初始化和销毁方法的方法名是任意的
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;Repository
//Scope(prototype)
public class BookDaoImpl implements BookDao {Overridepublic void save() {System.out.println(Book Dao Save...);}PostConstructpublic void init(){System.out.println(init...);}PreDestroypublic void destroy(){System.out.println(destroy);}}注意如果提示没找到注解的话需要在pom.xml中导入坐标
dependencygroupIdjavax.annotation/groupIdartifactIdjavax.annotation-api/artifactIdversion1.2/version
/dependency然后再执行主方法
package com.example.demo;import com.example.demo.config.SpringConfig;
import com.example.demo.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DemoApplication {public static void main(String[] args) {ApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);BookDao bookDao ctx.getBean(BookDao.class);System.out.println(bookDao);}
}执行的结果为
init...
com.example.demo.dao.impl.BookDaoImpl7eac9008没有调用Destroy方法的原因是没有关闭IoC容器/注册关闭钩子只需要像之前一样在程序结束前使用ctx.close()或通过ctx.registerShutdownHook()来注册关闭钩子 该方法注意不能使用ApplicationContext类因为没有close()方法和registerShutdownHook()方法
package com.example.demo;import com.example.demo.config.SpringConfig;
import com.example.demo.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class DemoApplication {public static void main(String[] args) {AnnotationConfigApplicationContext ctx new AnnotationConfigApplicationContext(SpringConfig.class);
// 方法二ctx.registerShutdownHook();BookDao bookDao ctx.getBean(BookDao.class);System.out.println(bookDao);// 方法一ctx.close();}
}注意非单例模式不会执行destroy因为多例模式下Spring不负责销毁。Spring默认是单例的它只管单例的销毁而不管多例的销毁
4.依赖注入
引用类型注入
依赖注入使用自动装配的方式这里阉割掉了配置文件中的Setter注入、构造注入等等一系列冗杂的方法使用自动装配。
Autowire属性默认按类型注入
这里注入很简单只需要在属性上加上Autowire注解就能够实现自动装配这里的自动装配是基于暴力反射实现的也无需再BookDaoImpl中实现set方法
package com.example.demo.service.impl;import com.example.demo.dao.BookDao;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;Service
public class BookServiceImpl implements BookService {AutowiredBookDao bookDao;Overridepublic void save() {System.out.println(Book Service Save...);bookDao.save();}
}假如我有两个类实现了BookDao.java这个类BookDaoImpl和BookDaoImpl2这时候再执行自动装配会报错因为基于类型反射得到的类不唯一
按类型注入反射结果不唯一按名注入
针对这种不唯一的情况给两个类的Repository注解后加名字BookDaoImpl和BookDaoImpl2分别为Repository(bookDao)和Repository(bookDao2)
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.stereotype.Repository;Repository(bookDao)
public class BookDaoImpl implements BookDao {Overridepublic void save() {System.out.println(Book Dao Save...);}
}package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.stereotype.Repository;Repository(bookDao2)
public class BookDaoImpl2 implements BookDao {Overridepublic void save() {System.out.println(Book Dao Save...2);}
}此时假如我们在Service中的属性定义是
Autowired
BookDao bookDao2;则会注入对应BookDaoImpl2这个类如果写的是bookDao1则会注入对应BookDaoImpl这个类 以上其实就是说明Spring注解开发中如果按类型注入不成功就会按名注入这些都是自动的无需人为配置按类型注入/按名注入
按名注入的标准写法少用
事实上按名注入更标准的方法是使用Qualifier注解这种注解还是要基于给BookDaoImpl和BookDaoImpl2都在Repository后指定了名称的情况下进行的标准写法如下
package com.example.demo.service.impl;import com.example.demo.dao.BookDao;
import com.example.demo.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;Service
public class BookServiceImpl implements BookService {AutowiredQualifier(bookDao2)BookDao bookDao;Overridepublic void save() {System.out.println(Book Service Save...);bookDao.save();}
}Qualifier后指定你想让bookDao属性被哪个类注入注意该注解必须搭配Autowired注解使用
简单类型注入
使用Value属性完成注入括号内写值
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;Repository
public class BookDaoImpl implements BookDao {Value(namenamename!!!)String name;Overridepublic void save() {System.out.println(Book Dao Save... this.name);}
}就这样的方法可能会有疑问这样写和String name namenamename!!!的区别是什么 不一样的地方在于你注解里面的这个字符串可能来自于外部比如来自于外部的properties文件
引入外部的properties文件
假如在Resource下有jdbc.properties文件有内容
namenamenamename!!!那么我们需要在配置类中加载这个文件加上注解PropertySource括号内是文件名
package com.example.demo.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;Configuration
ComponentScan(com.example.demo)
PropertySource(jdbc.properties)
public class SpringConfig {
}然后在值名称上写使用${}引用即可
package com.example.demo.dao.impl;import com.example.demo.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;Repository
public class BookDaoImpl implements BookDao {Value(${name})String name;Overridepublic void save() {System.out.println(Book Dao Save... this.name);}
}假如想加载多个配置文件使用{}来写即可类似于ComponentScan 但是注意之前在配置文件里加载的时候支持使用通配符*但这里完全不支持使用通配符 但是加上PropertySource(classpath:jdbc.properties)是可以的只是不能使用通配符 5. 第三方Bean
此处以数据库连接为例需要在pom.xml中导入坐标
dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.1.16/version
/dependency管理
在配置类中写一个方法一般方法名为Bean的ID。然后new一个第三方对象并返回。 在该方法上加上Bean注解表明该方法返回的是一个Bean
package com.example.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;Configuration
ComponentScan(com.example.demo)
public class SpringConfig {Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(com.mysql.jdbc.Driver);ds.setUrl(jdbc:mysql://localhost:3306/mysql);ds.setUsername(root);ds.setPassword(123456);return ds;}
}这样写就可以在主方法里像平常一样获得Bean
DataSource dataSource ctx.getBean(DataSource.class);
System.out.println(dataSource);但是将第三方的配置写在Spring的配置类中可能比较乱所以我们给它新建一个文件JdbcConfig.java
package com.example.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;Configuration
public class JdbcConfig {Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(com.mysql.jdbc.Driver);ds.setUrl(jdbc:mysql://localhost:3306/mysql);ds.setUsername(root);ds.setPassword(123456);return ds;}
}想要让这个文件被扫描到 第一种方法是在这个配置文件上加上注解Configuration然后在配置类中用ComponentScan()扫描这个包 ComponentScan(com.example.config) 第二种方法推荐使用无需在文件上加注解Configuration而是直接在配置类中加上Import注解并在括号内写这个新的配置文件名.classImport(JdbcConfig.class) 完整代码如下
package com.example.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;import javax.sql.DataSource;Configuration
//ComponentScan(com.example.demo.config)
Import(JdbcConfig.class)
public class SpringConfig {Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(com.mysql.jdbc.Driver);ds.setUrl(jdbc:mysql://localhost:3306/mysql);ds.setUsername(root);ds.setPassword(123456);return ds;}
}多个数据也使用数组模式
依赖注入
简单类型注入
只需要在类里面定义简单类型的成员变量并在方法中使用成员变量即可同样通过Value()注解赋值
package com.example.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;Configuration
public class JdbcConfig {Value(com.mysql.jdbc.Driver)String driverClassName;Value(jdbc:mysql://localhost:3306/mysql)String url;Value(root)String usernmae;Value(123456)String password;Beanpublic DataSource dataSource(){DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driverClassName);ds.setUrl(url);ds.setUsername(usernmae);ds.setPassword(password);return ds;}
}引用类型注入
假如要注入引用类型需要在方法的参数里面写引用类型
package com.example.demo.config;import com.alibaba.druid.pool.DruidDataSource;
import com.example.demo.dao.BookDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;Configuration
public class JdbcConfig {Value(com.mysql.jdbc.Driver)String driverClassName;Value(jdbc:mysql://localhost:3306/mysql)String url;Value(root)String usernmae;Value(123456)String password;Beanpublic DataSource dataSource(BookDao bookDao){bookDao.save();DruidDataSource ds new DruidDataSource();ds.setDriverClassName(driverClassName);ds.setUrl(url);ds.setUsername(usernmae);ds.setPassword(password);return ds;}
}因为我们已经注解了这个dataSource是个Bean所以对于方法里面的形参Spring会自动到IoC里面查找对应的类进行注入
6. XML配置比对注解配置
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83919.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!