Springframework (2)

zhaozj2021-02-16  85

First, Spring Foundation

1, core

(1) IOC / DepENDENCY INJECTION

l IOC / Dependency Injection: Beans does not depend on the framework; container is injectable

l Lightweight Spring Container: Configuration and Management Beans

(2) BeanFactory

l Lightweight bean container

l Load bean definition, including:

? Id / name

Class

Singleton or prototype

? Attributes

? Construct parameters

? Initialization method

? Destructure method

(3) XMLBeanFactory

l beanfactory implementation

l Beans definition:

l Use example:

InputStream INPUT = New FileInputStream ("Beans.xml");

Beanfactory factory = new xmlbeanfactory (input);

EXAMPLEBEAN EB = (Examplebean) Factory.getBean ("Examplebean");

EXAMPLEBEANTWO EB2 = (ExampleBeantwo) Factory.getBean ("Anotherexample");

May throw NosuchBeandefinitionException;

ExampleBean EB = (Examplebean) Factory.getBean ("EXAMPLEBEAN", ExampleBebean.class);

BEANNNOTOFREQUIREDTYPEEXCEPTION may throw

(4) Bean collaborators

l Other Beans required for your bean work

Package eg;

Public class examplebean {

PRIVATE ANOTHERBEAN Beanone;

Private Yetanotherbean Beantwo;

Public void setBeanone (anotherbean b) {beanone = b;}

Public void setBeantwo (YETANOTHERBEAN B) {beantwo = b;}

}

(5) Bean property

l Set the bean property

Package eg;

Public class examplebean {

PRIVATE STRING S;

Private INT i;

Public void setstringproperty (string s) {this.s = s;}

Public void setintegerProperty (int i) {this.i = i;}

}

hi!

7

0.25

True

0,255,0

Java.awt.color is initialized with RGB value

(8) Spring attribute editor

l example:

java.lang.object

/Home/ziba/file.txt

pt_br

http://java.net

Foo, Bar, Baz

(9) Customization attribute editor

l Date example:

DateFormat FMT = New SimpleDateFormat ("D / M / YYYY");

CustomDateEditor DateEditor = New CustomDateEditor (FMT, FALSE);

Beanfactory.register (java.util.date.class, dateeditor);

19/2/2004

l StringTrimming Example: Trim string, converted a null value

StringTrimmereditor Trimmer = new stringtrimmereditor (true);

Beanfactory.registercustomeditor (java.lang.string.class, trimmer);

Hello

(10) java.util.properties

l Properties example:

FOO = 1

Bar = 2

Baz = 3

1

2

3

(11) java.util.list

l List:

a List element

(12) java.util.set

l Set:

a set element

(13) java.util.map

l Map example:

Just some string

(14) Construction method

l Example of the construction method:

Public class examplebean {

PRIVATE ANOTHERBEAN Beanone;

Private Yetanotherbean Beantwo;

Private INT i;

Public ExampleBean (Anotherbean B1, YetAnotherbean B2, INT I) {

THIS.Beanone = B1;

This.beantwo = b2;

THIS.I = I;

}

}

1

(15) bean life cycle

l Beans can be initialized by Factory when used for the first time.

Public class examplebean {

Public void init () {

// Do some initialization work

}

}

l Beans can be cleared when it is no longer used

Public class examplebean {

Public void cleanup () {

// do some destruction work

}

}

(16) PropertyPlaceHolderConfigurer

l Input from the external Properties file

Class = "org.apache.commons.dbcp.basicdataasource"

Destroy-method = "close">

$ {jdbc.driverclassname}

$ {jdbc.url}

$ {jdbc.username}

$ {jdbc.password}

JDBC.Properties

Jdbc.driverclassname = org.hsqldb.jdbcdriver

JDBC.URL = JDBC: HSQLDB: HSQL: // Production: 9002

JDBC.USERNAME = SA

Jdbc.password = root

l Install Configurer

InputStream INPUT = New FileInputStream ("Beans.xml");

Xmlbeanfactory factory = new xmlbeanfactory (Input);

Properties PROPS = New Properties ();

ProPs.Load (New FileInputStream ("JDBC.Properties");

PropertyPlaceHolderConfigurer cfg = new profmentholderconfigurer ();

Cfg.SetProperties (PrOPS);

Cfg.PostProcessBeanFactory (Factory);

DataSource DS = (Datasource) Factory.getBean ("Datasource");

(17) MethodInvokingFactoryBean

l Exposure to use Singleton mode Bean

Package eg;

Public class mysingleton {

Private static mysingleton instance = new mysingleton ();

Private mysingleton () {}

Public static mysingleton getInstance () {

Return Instance;

}

}

Class = "... beans.factory.config.MethodInvokingFactoryBean">

eg.mysingleton.getInstance

FactoryBean creates beans on behalf of another class

(18) FactoryBean reference

l Get FactoryBean's own reference:

MYSINGLETON SINGLETON = (MySingleton) CTX.getBean ("MySingleton");

Get Bean created by Factory

FactoryBean Factory = (FactoryBean) CTX.getBean ("& mysingleton");

Get Factory

(19) Advanced features

l single case / prototype

l Autowiring: For Type, require each type of type with a single instance; for Name, the bean name (for non-simple attributes) of each attribute name is required.

l depending on the inspection

l beanwrapper

l INITIALIZINGBEAN / DISPOSABLEBEAN interface

l beanfactoryaware / beannameaware interface

转载请注明原文地址:https://www.9cbs.com/read-11817.html

New Post(0)