Java IoC and javaioc

Source: Internet
Author: User

Java IoC and javaioc

IoC, control inversion, is the core of spring. In other words, we don't have to use new to create objects by ourselves, and inject classes into IoC containers through l configuration. at startup, the IoC container helps us create objects and manage their dependencies. This implementation method is called DI and dependency injection. Why should we use IoC to help us manage objects? Generally, a business logic is done by multiple objects. If we manage them ourselves, we can compare them, it is necessary to call multiple objects in a method, which is difficult to manage and has low reusability.

// Add configuration files in factory mode to demonstrate the working principle of a simple IoC // 1. beanFactory. java bean Factory, create object // 2. propertiesReader. java reads the configuration file // 3. common bean class // 4. test class 
1 package myIoC; 2/** 3*1. the interface for reading configuration files has two implementation classes. One is to read the configuration file from the class loading path, and the other is to read the configuration file from the file path 4*2. bean Factory method provided 5 * @ author LH-PC 6*7 */8 public interface ApplicationContext {9 10/* 11 * bean Factory method 12 * @ param name13 * @ return14 */15 object getBean (String name ); 16 Object getBean (String name, String className); 17 18}
1 package myIoC; 2 3 import java. util. date; 4 import java. util. map; 5 6/** 7 * ApplicationContext implementation class, class load read configuration file 8 * @ author LH-PC 9*10 */11 public class ClassPathXmlApplicationContext implements ApplicationContext {12 13 private String propertiesName; 14 15/** 16 * constructor is used to load the configuration file 17 * @ param xml18 */19 public ClassPathXmlApplicationContext (String propertiesName) {20 // initialize attribute 21 this. propertiesName = propertiesName; 22} 23 24/** 25 * factory method through class loading 26 */27 public Object getBean (String name) {28 // read the configuration file 29 PropertiesReader propertiesReader = new PropertiesReader (propertiesName); 30 Object object = null; 31 // create Object 32 Map <String, String> map = propertiesReader. getProperties (); 33 try {34 System. err. println (new Date () + ":" + "BeanFactory starts to produce objects... "); 35 object = Class. forName (map. get (name )). newInstance (); 36 System. err. println (new Date () + ":" + "production object completed"); 37} catch (InstantiationException e) {38 System. err. println (new Date () + ":" + "Object creation exception"); 39 e. printStackTrace (); 40} catch (IllegalAccessException e) {41 System. err. println (new Date () + ":" + "IllegalAccessException exception"); 42 e. printStackTrace (); 43} catch (ClassNotFoundException e) {44 System. err. println (new Date () + ":" + "class loading exception"); 45 e. printStackTrace (); 46} 47 48 return object; 49} 50 51 public Object getBean (String name, String className) {52 // TODO Auto-generated method stub53 return null; 54} 55 56}
1 package myIoC; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. util. date; 6 import java. util. enumeration; 7 import java. util. hashMap; 8 import java. util. map; 9 import java. util. properties; 10 11 // read properties File class 12 public class PropertiesReader {13 14 private Properties pro = null; 15 private InputStream in = null; 16 private String propertiesName = null; 17 18 public Prop ErtiesReader (String propertiesName) {19 this. propertiesName = propertiesName; 20} 21 22 @ SuppressWarnings ("rawtypes") 23 public Map <String, String> getProperties () {24 Map <String, string> map = new HashMap <String, String> (); 25 pro = new Properties (); // create a properties object 26 try {27 System. err. println (new Date () + ":" + "start to read" + propertiesName + "... "); 28 in = PropertiesReader. class. getClassLoader (). getResou RceAsStream (propertiesName); // read the configuration file 29 if (in = null) {30 System through the reflection mechanism. err. println (new Date () + ":" + "Empty stream reading"); 31} else {32 // start loading file content 33 pro. load (in); 34 System. err. println (new Date () + ":" + propertiesName + "read successful"); 35} 36 Enumeration en = pro. propertyNames (); // iterator traverses pro37 while (en. hasMoreElements () {38 String key = (String) en. nextElement (); // traverse key39 String value = pro. getProperty (key); // retrieves data based on the key Value, put it in map40 map. put (key, value); 41} 42} catch (IOException e) {43 System. err. println (new Date () + ":" + "io exception"); 44 e. printStackTrace (); 45} 46 finally {47 if (in! = Null) {48 try {49 in. close (); // close the read stream 50 System. err. println (new Date () + ":" + "read stream closed successfully"); 51} catch (IOException e) {52 System. err. println (new Date () + ":" + "failed to close stream reading"); 53 e. printStackTrace (); 54} 55} 56} 57 return map; 58} 59 60 public static void main (String [] args) {61 PropertiesReader propertiesReader = new PropertiesReader ("myIoc/applicationContext. properties "); 62 Map <String, String> map = propertiesReader. getProperties (); 63 System. out. println ("OK"); 64} 65 66}
student=myIoC.Studentbean2=test.BeansImpl2bean3=test.BeansImpl3
1 package myIoC; 2 3/** 4 * MyIoC test class 5 * @ author LH-PC 6 */7 public class IoCTest {8 public static void main (String [] args) {9 ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("myIoc/applicationContext. properties "); 10 Student student = (Student) applicationContext. getBean ("student"); 11 student. setSid ("15301"); 12 student. setSname ("sea"); 13 System. out. println (student. getSid (); 14 System. out. println (student. getSname (); 15 16} 17 18} 19 20/** 21 * test object class 22 * @ author LH-PC23 * 24 */25 class Student {26 private String sname; 27 private String sid; 28 29 public String getSname () {30 return sname; 31} 32 33 public void setSname (String sname) {34 this. sname = sname; 35} 36 37 public String getSid () {38 return sid; 39} 40 41 public void setSid (String sid) {42 this. sid = sid; 43} 44 45}

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.