In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
The @ Configuration annotation serves the same purpose as spring-*.xml. @ Configuration is to completely cancel the xml configuration file and switch to annotations. A comparison will be made below:
The loading mode of beans
Loading method of spring-.xml: ClassPathXmlApplicationContext, FileSystemXmlApplicationContext, ContextLoaderListener (for WEB)
How to load pure annotations: AnnotationConfigApplicationContext, AnnotationConfigWebApplicationContext (for WEB)
@ Configuration and @ Bean
Xml configuration:
Public class Student {private Integer age; private String name; public void setAge (Integer age) {this.age = age;} public Integer getAge () {System.out.println ("Age:" + age); return age;} public void setName (String name) {this.name = name;} public String getName () {System.out.println ("Name:" + name) Return name;} public void printThrowException () {System.out.println ("Exception raised"); throw new IllegalArgumentException ();} public void init () {System.out.println ("= Student.init=");} public void destroy () {System.out.println ("= Student.destroy=");}} ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("classpath:spring-bean.xml") Student student = context.getBean (Student.class); student.getName (); student.getAge ()
Note configuration:
@ Configuration@Lazy//@Profile ("test") public class BeanConfiguration {@ Bean (name = "student", initMethod = "init", destroyMethod = "destroy") @ Scope ("prototype") public Student student () {Student student = new Student (); student.setAge (100); student.setName ("this is a test name."); return student;} AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext () AnnotationConfigApplicationContext.register (BeanConfiguration.class); annotationConfigApplicationContext.refresh (); Student student1 = annotationConfigApplicationContext.getBean (Student.class); student1.getName (); student1.getAge ()
Half xml configuration and half annotations:
@ Configuration@Lazy//@Profile ("test") public class BeanConfiguration {@ Bean (name = "student", initMethod = "init", destroyMethod = "destroy") @ Scope ("prototype") public Student student () {Student student = new Student (); student.setAge (100); student.setName ("this is a test name."); return student }} ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext ("classpath:spring-application.xml"); Student student2 = context.getBean (Student.class); student2.getName (); student2.getAge (); @ Configuration and @ ComponentScan
Xml configuration:
@ Componentpublic class Teacher {private Integer age; private String name; private String id;. } ClassPathXmlApplicationContext context4 = new ClassPathXmlApplicationContext ("classpath:spring-annotation-componentScan.xml"); Teacher teacher4 = context4.getBean (Teacher.class); System.out.println ("teacher4.hashCode = [" + teacher4.hashCode () + "]")
Note configuration:
@ Configuration@ComponentScan ("com.demo") public class ComponentScanConfig {} AnnotationConfigApplicationContext context5 = new AnnotationConfigApplicationContext (); context5.register (ComponentScanConfig.class); context5.refresh (); Teacher teacher5 = context5.getBean (Teacher.class); System.out.println ("teacher5.hashCode = [" + teacher5.hashCode () + "]"); @ Configuration and @ PropertySource
Jdbc.properties:
Jdbc.url=jdbc:mysql://172.28.1.1:3306/testjdbc.username=testjdbc.password=123456jdbc.driverClassName=com.mysql.jdbc.Driverdruid.initialSize=10druid.minIdle=5druid.maxActive=20druid.maxWait=60000druid.poolPreparedStatements=truedruid.maxPoolPreparedStatementPerConnectionSize=33druid.timeBetweenEvictionRunsMillis=60000druid.minEvictableIdleTimeMillis=300000druid.validationQuery=select 1 from dualdruid.testWhileIdle=truedruid.testOnBorrow=falsedruid.testOnReturn=falsedruid.removeAbandoned=truedruid.removeAbandonedTimeout=1800druid.logAbandoned=truedruid.filters=stat,wall,slf4jdruid.logSlowSql=truedruid.loginUsername=testdruid.loginPassword=123456
Xml configuration:
ClassPathXmlApplicationContext context6 = new ClassPathXmlApplicationContext ("classpath:spring-data.xml"); DataSource dataSource = context6.getBean (DataSource.class); DruidDataSource druidDataSource = (DruidDataSource) dataSource; System.out.println ("url = [" + druidDataSource.getUrl () + "]"); System.out.println ("username = [" + druidDataSource.getUsername () + "]")
Note configuration:
@ Configuration@PropertySource ("classpath:jdbc.properties") public class DruidJdbcConfig {@ Value ("${jdbc.url}") private String url; @ Value ("${jdbc.username}") private String username; @ Value ("${jdbc.password}") private String password; @ Value ("${jdbc.driverClassName}") private String driverClassName; @ Value ("${druid.initialSize}") private int initialSize @ Value ("${druid.minIdle}") private int minIdle; @ Value ("${druid.maxActive}") private int maxActive; @ Value ("${druid.maxWait}") private int maxWait; @ Value ("${druid.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @ Value ("${druid.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @ Value ("${druid.validationQuery}") private String validationQuery @ Value ("${druid.testWhileIdle}") private boolean testWhileIdle; @ Value ("${druid.testOnBorrow}") private boolean testOnBorrow; @ Value ("${druid.testOnReturn}") private boolean testOnReturn; @ Value ("${druid.removeAbandoned}") private boolean removeAbandoned; @ Value ("${druid.removeAbandonedTimeout}") private int removeAbandonedTimeout; @ Value ("${druid.logAbandoned}") private boolean logAbandoned @ Value ("${druid.filters}") private String filters; @ Value ("${druid.logSlowSql}") private boolean logSlowSql; @ Value ("${druid.loginUsername}") private String loginUsername; @ Value ("${druid.loginPassword}") private String loginPassword;} AnnotationConfigApplicationContext context7 = new AnnotationConfigApplicationContext (); context7.register (DruidJdbcConfig.class); context7.refresh (); DruidJdbcConfig druidJdbcConfig = context7.getBean (DruidJdbcConfig.class) System.out.println ("url = [" + druidJdbcConfig.getUrl () + "]"); System.out.println ("username = [" + druidJdbcConfig.getUsername () + "]"); @ Configuration and @ Import
Spring-application.xml
Spring-data.xml
ClassPathXmlApplicationContext context8 = new ClassPathXmlApplicationContext ("classpath:spring-application.xml"); query (context8.getBean (DataSource.class))
Note configuration:
@ Configuration@PropertySource ("classpath:jdbc.properties") public class DruidJdbcConfig {@ Value ("${jdbc.url}") private String url; @ Value ("${jdbc.username}") private String username; @ Value ("${jdbc.password}") private String password; @ Value ("${jdbc.driverClassName}") private String driverClassName; @ Value ("${druid.initialSize}") private int initialSize @ Value ("${druid.minIdle}") private int minIdle; @ Value ("${druid.maxActive}") private int maxActive; @ Value ("${druid.maxWait}") private int maxWait; @ Value ("${druid.timeBetweenEvictionRunsMillis}") private int timeBetweenEvictionRunsMillis; @ Value ("${druid.minEvictableIdleTimeMillis}") private int minEvictableIdleTimeMillis; @ Value ("${druid.validationQuery}") private String validationQuery @ Value ("${druid.testWhileIdle}") private boolean testWhileIdle; @ Value ("${druid.testOnBorrow}") private boolean testOnBorrow; @ Value ("${druid.testOnReturn}") private boolean testOnReturn; @ Value ("${druid.removeAbandoned}") private boolean removeAbandoned; @ Value ("${druid.removeAbandonedTimeout}") private int removeAbandonedTimeout; @ Value ("${druid.logAbandoned}") private boolean logAbandoned @ Value ("${druid.filters}") private String filters; @ Value ("${druid.logSlowSql}") private boolean logSlowSql; @ Value ("${druid.loginUsername}") private String loginUsername; @ Value ("${druid.loginPassword}") private String loginPassword;} @ Configuration@Import (DruidJdbcConfig.class) public class DruidPoolConfig {private final static Logger LOGGER = LoggerFactory.getLogger (DruidPoolConfig.class); @ Autowired private DruidJdbcConfig druidJdbcConfig @ Bean public DataSource dataSource () {DruidDataSource datasource = new DruidDataSource (); datasource.setUrl (druidJdbcConfig.getUrl ()); datasource.setUsername (druidJdbcConfig.getUsername ()); datasource.setPassword (druidJdbcConfig.getPassword ()); datasource.setDriverClassName (druidJdbcConfig.getDriverClassName ()); datasource.setInitialSize (druidJdbcConfig.getInitialSize ()); datasource.setMinIdle (druidJdbcConfig.getMinIdle ()); datasource.setMaxActive (druidJdbcConfig.getMaxActive ()) Datasource.setMaxWait (druidJdbcConfig.getMaxWait ()); datasource.setTimeBetweenEvictionRunsMillis (druidJdbcConfig.getTimeBetweenEvictionRunsMillis ()); datasource.setMinEvictableIdleTimeMillis (druidJdbcConfig.getMinEvictableIdleTimeMillis ()); datasource.setValidationQuery (druidJdbcConfig.getValidationQuery ()); datasource.setTestWhileIdle (druidJdbcConfig.isTestWhileIdle ()); datasource.setTestOnBorrow (druidJdbcConfig.isTestOnBorrow ()); datasource.setTestOnReturn (druidJdbcConfig.isTestOnReturn ()); datasource.setRemoveAbandoned (druidJdbcConfig.isRemoveAbandoned ()) Datasource.setRemoveAbandonedTimeout (druidJdbcConfig.getRemoveAbandonedTimeout ()); datasource.setLogAbandoned (druidJdbcConfig.isLogAbandoned ()); try {datasource.setFilters (druidJdbcConfig.getFilters ());} catch (SQLException e) {LOGGER.error ("datasource.setFilters occur error.", e);} return datasource;}} AnnotationConfigApplicationContext context9 = new AnnotationConfigApplicationContext (); context9.register (DruidPoolConfig.class) Context9.refresh (); query (context9.getBean (DataSource.class)); public static void query (DataSource dataSource) throws SQLException {Connection connection = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try {connection = dataSource.getConnection (); connection.setAutoCommit (false); String sql = "select id, `name`, mobile from t_agent_user where id =?" PreparedStatement = connection.prepareStatement (sql); preparedStatement.setInt (1, 6497); resultSet = preparedStatement.executeQuery (); while (resultSet.next ()) {int id = resultSet.getInt ("id"); String name = resultSet.getString ("name"); String mobile = resultSet.getString ("mobile") System.out.println ("id = [" + id + "]"); System.out.println ("name = [" + name + "]"); System.out.println ("mobile = [" + mobile + "]");} catch (SQLException e) {e.printStackTrace () If (connection! = null) connection.rollback ();} finally {if (connection! = null) {connection.commit (); connection.setAutoCommit (true);} if (resultSet! = null) resultSet.close () If (preparedStatement! = null) preparedStatement.close (); if (connection! = null) connection.close ();} @ Configuration and @ ImportResource@Configuration@ImportResource ("classpath:spring-data.xml") public class ImportResourceConfig {} AnnotationConfigApplicationContext context10 = new AnnotationConfigApplicationContext (); context10.register (ImportResourceConfig.class); context10.refresh () Query (context10.getBean (DataSource.class))
Reference: https://www.cnblogs.com/duanxz/p/7493276.html
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.